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: "unterminated subpattern" in valid regex if re.VERBOSE is used
Type: Stage: resolved
Components: Regular Expressions Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, mrabarnett, nedbat, serhiy.storchaka
Priority: normal Keywords:

Created on 2022-03-30 11:49 by nedbat, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg416340 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2022-03-30 11:49
In this code, the same regex is compiled with and without re.VERBOSE.  Without, it compiles fine.  With, it fails with an "unterminated subpattern" error.

    list_num_rx1 =     r"""(?P<paren>\()?(\d+|#|[a-z])(?(paren)\)|\.)"""
    list_num_rx2 = r"""(?x)(?P<paren>\()?(\d+|#|[a-z])(?(paren)\)|\.)"""
    
    # This works:
    re.compile(list_num_rx1)
    # Either of these fails:
    re.compile(list_num_rx1, flags=re.VERBOSE)
    re.compile(list_num_rx2)

(What I really wanted was this, but the error happens without the multiline string:)

    list_num_rx = r"""(?x)
        (?P<paren>\()?          # maybe an opening paren
        (\d+|#|[a-z])           # the number: 123, or #, or a-z
        (?(paren)               # if we had an opening paren..
            \)|                 #   then we need a closing paren
            \.                  #   otherwise a dot.
        )
        """
msg416341 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2022-03-30 11:57
I realized my mistake... never mind!
msg416343 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-03-30 11:58
"#" in VERBOSE mode starts a comment and skips to the end of the line. Escape it: "\#".
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91319
2022-03-30 11:59:31serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2022-03-30 11:58:40serhiy.storchakasetstatus: closed -> open

nosy: + serhiy.storchaka
messages: + msg416343

resolution: not a bug -> (no value)
stage: resolved -> (no value)
2022-03-30 11:57:51nedbatsetstatus: open -> closed
resolution: not a bug
messages: + msg416341

stage: resolved
2022-03-30 11:49:35nedbatcreate