The short option is that you cannot use variable-width patterns in lookbehinds with the Python re module. Unable to change this:
>>> import re >>> re.sub("(?<=foo)bar(?=baz)", "quux", "foobarbaz") 'fooquuxbaz' >>> re.sub("(?<=fo+)bar(?=baz)", "quux", "foobarbaz") Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> re.sub("(?<=fo+)bar(?=baz)", "quux", string) File "C:\Development\Python25\lib\re.py", line 150, in sub return _compile(pattern, 0).sub(repl, string, count) File "C:\Development\Python25\lib\re.py", line 241, in _compile raise error, v # invalid expression error: look-behind requires fixed-width pattern
This means that you need to get around this, the simplest solution, very similar to what you are doing now:
>>> re.sub("(fo+)bar(?=baz)", "\\1quux", "foobarbaz") 'fooquuxbaz' >>> >>>
It does not have the elegance of a lookbehind solution, but it is still very clear, simple, single-line. And if you look at what the expert has to say on this issue (he talks about JavaScript, which lacks views completely, but many of the principles are the same), you will see that its simplest solution is very similar to this.
Ben Blank Jan 29 '09 at 15:11 2009-01-29 15:11
source share