python / python/cpython

Support redefinition of named capture groups in regular expressions

Đang mở
#152,026 3 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

extension-modules topic-regex type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với module re của Python và công cụ khớp SRE được mô tả trong issue, sau đó xem xét hành vi lưu/khôi phục mark hiện có bên trong các phép lặp. Công việc được hoàn thành khi các nhóm bắt có tên có thể được sử dụng lại giữa các nhánh thay thế, dùng chung một số nhóm và duy trì các kết quả bắt đúng trong quá trình backtracking mà không ảnh hưởng đến các pattern khác.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
20/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.