I get an error message when I use this expression:
re.sub(r"([^\s\w])(\s*\1)+","\\1","...")
I checked the regex at RegExr and it returns .
as expected. But when I try it in Python I get this error message:
raise error, v # invalid expression sre_constants.error: nothing to repeat
Can someone please explain?
It seems to be a python bug (that works perfectly in vim). The source of the problem is the (\s*...)+ bit. Basically , you can't do (\s*)+
which make sense , because you are trying to repeat something which can be null.
>>> re.compile(r"(\s*)+") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 180, in compile return _compile(pattern, flags) File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 233, in _compile raise error, v # invalid expression sre_constants.error: nothing to repeat
However (\s*\1)
should not be null, but we know it only because we know what's in \1. Apparently python doesn't ... that's weird.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With