classification
Title: Some input chars (i.e. '++') break re.match
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, jpfisher, mrabarnett
Priority: normal Keywords:

Created on 2014-08-01 20:44 by jpfisher, last changed 2014-08-01 21:27 by ezio.melotti. This issue is now closed.

Messages (2)
msg224518 - (view) Author: John Fisher (jpfisher) Date: 2014-08-01 20:44
Some characters repeated in the pattern break re.match:


Linux python 2.7.6
###################################
# test.py
import re

#diffitem = "libstdc+"   succeeds
#diffitem = "libstdc++"  fails
#diffitem = "libstdc**"  fails
#diffitem = "libstdc.."  succeeds
diffitem = "libstdc+\+"  succeeds
line = "time  1.7-23build1"

result = re.match(diffitem, line)
print result

###################################
$ python  test.py
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    result = re.match(diffitem, line)
  File "/usr/lib/python2.7/re.py", line 137, in match
    return _compile(pattern, flags).match(string)
  File "/usr/lib/python2.7/re.py", line 244, in _compile
    raise error, v # invalid expression
sre_constants.error: multiple repeat
msg224521 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2014-08-01 21:19
In a regex, '+' is a metacharacter meaning "repeated one or more times".

"libstdc+" will match "libstd" followed by "c" repeated one or more times.

"libstdc++" will match "libstd" followed by "c" repeated one or more times, but then there's another "+", which it takes to mean that you want the repeat to be repeated, hence the exception.

'*' is also a metacharacter, this one meaning "repeated zero or more times".

In summary, not a bug.
History
Date User Action Args
2014-08-01 21:27:18ezio.melottisetstatus: open -> closed
type: compile error -> behavior
resolution: not a bug
stage: resolved
2014-08-01 21:19:27mrabarnettsetmessages: + msg224521
2014-08-01 20:44:52jpfishercreate