This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: re.compile fails for verbose re with more than one group
Type: Stage:
Components: Regular Expressions Versions: Python 2.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: effbot Nosy List: effbot, gcorral, glchapman, mwh
Priority: normal Keywords:

Created on 2003-01-24 20:36 by gcorral, last changed 2022-04-10 16:06 by admin. This issue is now closed.

Messages (3)
msg14199 - (view) Author: Garth Corral (gcorral) Date: 2003-01-24 20:36
The third of the following three regular expressions
fails to compile with "unbalanced parenthesis" error:


[foo.py]

import re

_good_re =
re.compile(r'\s*(elif|else|except|finally).*:\s*(#.*)$')

_also_good_re = re.compile(r'''
    \s*
    ( elif
    | else
    | except
    | finally
    )
    .*:
''', re.VERBOSE)

_bad_re = re.compile(r'''
    \s*
    ( elif
    | else
    | except
    | finally
    )
    .*:\s*(#.*)?$
''', re.VERBOSE)


>>> import foo
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "zzz.py", line 15, in ?
    _bad_re = re.compile(r'''
  File "/usr/local/lib/python2.2/sre.py", line 178, in
compile
    return _compile(pattern, flags)
  File "/usr/local/lib/python2.2/sre.py", line 228, in
_compile
    raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis
msg14200 - (view) Author: Greg Chapman (glchapman) Date: 2003-02-05 02:35
Logged In: YES 
user_id=86307

The problem here is the '#' character in the third pattern.  When using re.VERBOSE, it is taken as beginning a comment (unless preceeded by a backslash).  Change that line to:

.*:\s*(\#.*)?$

and the pattern will compile.
msg14201 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2003-02-05 14:12
Logged In: YES 
user_id=6656

Ta.
History
Date User Action Args
2022-04-10 16:06:10adminsetgithub: 37827
2003-01-24 20:36:28gcorralcreate