re.compile bug?
Tom McDermott
spon at cisco.com
Wed Dec 19 18:22:35 EST 2001
John Dell'Aquila wrote:
>
> Compilation fails with VERBOSE flag but works otherwise.
>
> bash-2.05$ python
> ActivePython 2.1.1, build 212 (ActiveState)
> Python 2.1.1 (#20, Jul 26 2001, 11:38:51) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> >>> import re
> >>> comment = "([^#]*)(#.*)?$"
> >>> re.compile(comment)
> <SRE_Pattern object at 00814050>
> >>> re.compile(comment, re.VERBOSE)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "d:\PYTHON21\lib\sre.py", line 90, in compile
> return _compile(pattern, flags)
> File "d:\PYTHON21\lib\sre.py", line 136, in _compile
> raise error, v # invalid expression
> sre_constants.error: unbalanced parenthesis
> >>>
> --
> John Dell'Aquila
John,
From http://python.org/doc/current/lib/Contents_of_Module_re.html:
X
VERBOSE
This flag allows you to write regular expressions that look nicer.
Whitespace within the pattern is ignored, except when in a
character
class or preceded by an unescaped backslash, and, when a line
contains
a "#" neither in a character class or preceded by an unescaped
backslash, all characters from the leftmost such "#" through the
end
of the line are ignored.
So - the re is recognizing the second hash as a comment delimiter,
and the second set of parens is now unbalanced.
Solution: protect the hash with a backslash:
comment = "([^#]*)(\#.*)?$"
Cheers,
Tom
More information about the Python-list
mailing list