Python regex: error: don't repeat anything

I use re.sub() to remove color management codes (IRC) from the string, but it fails every time. This is what I use:

 re.sub('\x03(\d*)?,?(\d*)?', '', content) 

Error:

  File "/usr/lib/python2.7/re.py", line 151, in sub return _compile(pattern, flags).sub(repl, string, count) File "/usr/lib/python2.7/re.py", line 242, in _compile raise error, v # invalid expression error: nothing to repeat 

I don't see anything wrong with my regex, so can someone explain what I'm doing wrong?

+4
source share
1 answer

Your regex is legal, but the Python regex implementation has an error that causes it to reject nested optional quantifiers in some cases.

You still do not need (...)? es:

 re.sub('\x03\\d*,?\\d*', '', content) 
+7
source

Source: https://habr.com/ru/post/1497782/


All Articles