Issue 14069: In extensions (?...) the lookbehind assertion cannot choose between the beginning of string and a letter (original) (raw)
import re re.search(r'(?<=(a|b))(\w+)', 'abc').groups() ('a', 'bc') re.search(r'(?<=(^))(\w+)', 'abc').groups() ('', 'abc') re.search(r'(?<=(^|$))(\w+)', 'abc').groups() ('', 'abc') re.search(r'(?<=($|^))(\w+)', 'abc').groups() ('', 'abc') re.search(r'(?<=(^|a))(\w+)', 'abc').groups() Traceback (most recent call last): File "/usr/local/lib/python3.2/functools.py", line 176, in wrapper result = cache[key] KeyError: (<class 'str'>, '(?<=(^|a))(\w+)', 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.2/re.py", line 158, in search return _compile(pattern, flags).search(string) File "/usr/local/lib/python3.2/re.py", line 255, in _compile return _compile_typed(type(pattern), pattern, flags) File "/usr/local/lib/python3.2/functools.py", line 180, in wrapper result = user_function(*args, **kwds) File "/usr/local/lib/python3.2/re.py", line 267, in _compile_typed return sre_compile.compile(pattern, flags) File "/usr/local/lib/python3.2/sre_compile.py", line 495, in compile code = _code(p, flags) File "/usr/local/lib/python3.2/sre_compile.py", line 480, in _code _compile(code, p.data, flags) File "/usr/local/lib/python3.2/sre_compile.py", line 115, in _compile raise error("look-behind requires fixed-width pattern") sre_constants.error: look-behind requires fixed-width pattern
I believe this is not a bug. As the error says a look-behind requires the pattern to have a fixed length.
In re.search(r'(?<=(a|b))(\w+)', 'abc').groups() the two possible patterns in the look behind are "a" and "b", both with length 1. In re.search(r'(?<=(^|$))(\w+)', 'abc').groups() the two possible patterns in the look behind are "^" and "$", both with length 0.
In re.search(r'(?<=(^|a))(\w+)', 'abc').groups() the two possible patterns in the look behind are "^" and "a", one with length 0 and the other with length 1. This is not allowed and raises an error. Similarly, in re.search(r'(?<=(ab|a))(\w+)', 'abc').groups() the two patterns have length 2 and 1, and the same error is raised.
Closing as not a bug.