Support redefinition of named capture groups in regular expressions
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
Description
Feature or enhancement
Currently the re module rejects any reuse of a group name with "redefinition of group name 'x' as group N; was group M". This prevents the natural idiom of giving the same name to corresponding groups in alternative spellings of a pattern.
For example, parsing a date written in either order currently cannot reuse names:
re.compile(r'(?P<m>\d+)/(?P<d>\d+)/(?P<y>\d+)|(?P<y>\d+)-(?P<m>\d+)-(?P<d>\d+)')
# error: redefinition of group name 'y' as group 4; was group 3
Proposal
Allow a group name to be used for more than one group. All such groups share a single group number; the name (and that number) refer to whichever of the groups matched, or to the last one if more than one matched. The group's width spans the union of the definitions.
This matches the behavior of the third-party regex module and of PCRE's duplicate-names feature ((?J)), whose own canonical example — matching a weekday written as an abbreviation or in full and extracting it under one name — is exactly this alternative-spellings case.
Implementation note
This also needs a small fix in the SRE matching engine. Outside of repeats the matcher restores capture-group marks lazily (it only rewinds 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: The span of capturing group is wrong. Reused group numbers can be 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 mark save/restore flag also subsumes the existing workaround for the possessive-quantifier SystemError of gh-101955 (which currently installs a placeholder repeat context for the same purpose).
I have a working implementation and will open a PR.
Linked PRs
- gh-152027
- gh-153160
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par le module re de Python et le moteur de correspondance SRE décrit dans l’issue, puis examinez le comportement existant de sauvegarde et de restauration des marqueurs à l’intérieur des répétitions. La tâche sera terminée lorsque les groupes de capture nommés pourront être réutilisés entre les alternatives, partager un numéro de groupe et préserver les captures correctes lors du backtracking sans affecter les autres motifs.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- backend
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 20/100