Skip to content

gh-152026: Support redefinition of named capture groups#152027

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:re-redefined-named-groups
Open

gh-152026: Support redefinition of named capture groups#152027
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:re-redefined-named-groups

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

The same name can now be used for more than one capturing group in a regular expression. All such groups share a single group number; the name and number refer to whichever of the groups matched (the last one, if more than one matched). This is chiefly useful for giving the same name to corresponding groups in alternative spellings of a pattern, e.g. (?P<m>\d+)/(?P<d>\d+)/(?P<y>\d+)|(?P<y>\d+)-(?P<m>\d+)-(?P<d>\d+). Previously each group name had to be unique within a regular expression.

Sharing a group number also needs a fix in the SRE matching engine: outside of repeats it restores capture-group marks lazily (only rewinding the lastmark high-water index), which assumed each group number is written at a single place in the bytecode. Reused group numbers are now detected during code validation, and such patterns save and restore the full mark array on backtracking, as is already done inside repeats; other patterns are unaffected. The same flag also lets the possessive-quantifier handler request mark saving directly, replacing the placeholder repeat context previously installed for that purpose (gh-101955), so a possessive repeat no longer allocates a repeat context.

🤖 Generated with Claude Code

The same name can now be used for more than one capturing group.  All such
groups share a single group number; the name and number refer to whichever
of the groups matched (the last one, if more than one matched).  The group's
width range spans all the definitions.

This is chiefly useful for giving the same name to corresponding groups in
alternative spellings of a pattern:

    (?P<m>\d+)/(?P<d>\d+)/(?P<y>\d+)|(?P<y>\d+)-(?P<m>\d+)-(?P<d>\d+)

Previously each group name had to be unique within a regular expression.

Sharing a group number also requires a fix in the matching engine.  Outside
of repeats the matcher restores capture-group marks lazily, only rewinding
the lastmark high-water index, which assumes each group number is written at
a single place in the bytecode.  A reused group number could otherwise leak a
mark from a branch that matched and was then backtracked away, or raise
SystemError.  Reused group numbers are detected during code validation, and
such patterns save and restore the full mark array on backtracking, exactly
as is already done inside repeats; other patterns are unaffected.

The same flag also lets the possessive quantifier handler request mark saving
directly, replacing the placeholder repeat context that was previously
installed for that purpose (the pythongh-101955 workaround), so a possessive repeat
no longer allocates a repeat context.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33276899 | 📁 Comparing 0e91850 against main (7928a8b)

  🔍 Preview build  

3 files changed
± library/re.html
± whatsnew/3.16.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant