Anomalous behaviour when compiling regular expressions?

Harvey.Thomas at informa.com Harvey.Thomas at informa.com
Mon Mar 13 05:32:33 EST 2006


>>> import re
>>> r = re.compile('(a|b*)+')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "c:\python24\lib\sre.py", line 180, in compile
    return _compile(pattern, flags)
  File "c:\python24\lib\sre.py", line 227, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat

but

>>> r = re.compile('(a|b*c*)+')
>>> r.match('def').group()
''

Why is there a difference in behaviour between the two cases. Surely the
two cases are equivalent to:

>>> r = re.compile('(a|b)*')
>>> r.match('def').group()
''

and 

>>> r = re.compile('(a|b|c)*')
>>> r.match('def').group()
''

Harvey

--------------------------------------------------------------------------------------------------------------------------------------------
The information contained in this email message may be confidential. If you are not the intended recipient, any use, interference with, disclosure or copying of this material is unauthorised and prohibited. Although this message and any attachments are believed to be free of viruses, no responsibility is accepted by Informa for any loss or damage arising in any way from receipt or use thereof.  Messages to and from the company are monitored for operational reasons and in accordance with lawful business practices. 
If you have received this message in error, please notify us by return and delete the message and any attachments.  Further enquiries/returns can be sent to postmaster at informa.com



More information about the Python-list mailing list