You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gh-152026: Support redefinition of named capture groups
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 gh-101955 workaround), so a possessive repeat
no longer allocates a repeat context.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
0 commit comments