[New-bugs-announce] [issue47163] "unterminated subpattern" in valid regex if re.VERBOSE is used

Ned Batchelder report at bugs.python.org
Wed Mar 30 07:49:35 EDT 2022

New submission from Ned Batchelder <ned at nedbatchelder.com>:

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:
    # Either of these fails:
    re.compile(list_num_rx1, flags=re.VERBOSE)

(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.

components: Regular Expressions
messages: 416340
nosy: ezio.melotti, mrabarnett, nedbat
priority: normal
severity: normal
status: open
title: "unterminated subpattern" in valid regex if re.VERBOSE is used

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list