classification
Title: In extensions (?...) the lookbehind assertion cannot choose between the beginning of string and a letter
Type: behavior Stage: resolved
Components: Library (Lib), Regular Expressions Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, ezio.melotti, py.user
Priority: normal Keywords:

Created on 2012-02-21 02:55 by py.user, last changed 2014-07-03 16:47 by ezio.melotti. This issue is now closed.

Messages (3)
msg153836 - (view) Author: py.user (py.user) * Date: 2012-02-21 02:55
>>> 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 "<stdin>", line 1, in <module>
  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
>>>
msg222129 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-02 21:25
@py.user please accept our apologies for having missed this.

@Ezio can you comment on this please.
msg222199 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-07-03 16:47
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.
History
Date User Action Args
2014-07-03 16:47:35ezio.melottisetstatus: open -> closed
resolution: not a bug
messages: + msg222199

stage: resolved
2014-07-02 21:25:27BreamoreBoysetnosy: + BreamoreBoy
messages: + msg222129
2012-02-21 02:55:58py.usercreate