[Python-bugs-list] [ python-Bugs-674264 ] re.compile fails for verbose re with more than one group

SourceForge.net noreply@sourceforge.net
Wed, 05 Feb 2003 06:12:57 -0800


Bugs item #674264, was opened at 2003-01-24 20:36
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=674264&group_id=5470

Category: Regular Expressions
Group: Python 2.2.1
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Garth Corral (gcorral)
Assigned to: Fredrik Lundh (effbot)
Summary: re.compile fails for verbose re with more than one group

Initial Comment:
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


----------------------------------------------------------------------

>Comment By: Michael Hudson (mwh)
Date: 2003-02-05 14:12

Message:
Logged In: YES 
user_id=6656

Ta.

----------------------------------------------------------------------

Comment By: Greg Chapman (glchapman)
Date: 2003-02-05 02:35

Message:
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.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=674264&group_id=5470