Anomalous behaviour when compiling regular expressions?

Harvey.Thomas at Harvey.Thomas at
Mon Mar 13 11:32:33 CET 2006

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


>>> 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()


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


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

More information about the Python-list mailing list