A bug in Python's regular expression engine?

Diez B. Roggisch deets at nospam.web.de
Tue Nov 27 16:52:18 CET 2007

Just Another Victim of the Ambient Morality wrote:

>     This won't compile for me:
> regex = re.compile('(.*\\).*')
>     I get the error:
> sre_constants.error: unbalanced parenthesis
>     I'm running Python 2.5 on WinXP.  I've tried this expression with
> another RE engine in another language and it works just fine which leads
> me
> to believe the problem is Python.  Can anyone confirm or deny this bug?

It pretty much says what the problem is - you escaped the closing
parenthesis, resulting in an invalid rex.

Either use raw-strings or put the proper amount of backslashes in your

regex = re.compile(r'(.*\\).*') # raw string literal

regex = re.compile('(.*\\\\).*') # two consecutive \es, meaning an escaped


More information about the Python-list mailing list