re Compile - a bug?

Skip Montanaro skip at mojam.com
Wed Feb 16 11:49:07 EST 2000


    Colin> hpat= compile(r'''
    Colin>   (<(?P<Hdr>th) [^>]*>)           #  Either header up to terminating '>'
    Colin>                                   # |(?P<Hdr>td)[^>]*>)  <-- Grief!
    Colin>   (.*?)                           #  Followed by anything
    Colin>   </(?P=Hdr)>",flags + VERBOSE    # Matching terminator
    Colin>   ''')
    Colin>                -------------------------------
    Colin> Correctly, the PythonWin editor does not report the mismatch.

In triple-quoted strings what you think are comments are not.  They are part
of the string.  Also, it appears you close your string after the flags
argument for re.compile instead of before it.  The following might work
better:

    hpat= compile(
      r'(<(?P<Hdr>th) [^>]*>)'           #  Either header up to terminating '>'
                                         #  |(?P<Hdr>td)[^>]*>)  <-- Grief!
      r'(.*?)'                           #  Followed by anything
      r'</(?P=Hdr)>',                    #  Matching terminator
      flags + VERBOSE)

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
"Languages that change by catering to the tastes of non-users tend not to do
so well." - Doug Landauer





More information about the Python-list mailing list