re.compile bug?

Skip Montanaro skip at pobox.com
Wed Dec 19 17:40:53 EST 2001


    John> Compilation fails with VERBOSE flag but works otherwise.
    ...
    >>> comment = "([^#]*)(#.*)?$"
    >>> 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

Note that when verbose mode is enabled, the syntax of regular expressions
changes slightly to make them more readable (that is "verbose" doesn't mean
"verbose output", but "verbose input"):

    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.

All you need to do is place your second hash mark in a character class to
keep it from being interpreted as an embedded comment:

    >>> comment = "([^#]*)([#].*)?$"
    >>> import re
    >>> re.compile(comment, re.VERBOSE)
    <_sre.SRE_Pattern object at 0x81db008>

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list