-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-86519: Add prefixmatch APIs to the re module #31137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gpshead
wants to merge
11
commits into
python:main
Choose a base branch
from
gpshead:prefixmatch-b42353
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
These alleviate common confusion around what "match" means as Python is different than other popular languages in our use of the term as an API name. The original "match" names are NOT being deprecated. Source tooling like linters are expected to suggest using prefixmatch instead of match to improve code health and reduce cognitive burden of understanding the intent when reading code. See the documentation changes within this PR for a better description.
554bb41 to
54c77ca
Compare
`match = prefixmatch`
54c77ca to
149f6e4
Compare
ntBre
pushed a commit
to astral-sh/ruff
that referenced
this pull request
Jan 30, 2026
## Summary The example for [FURB167](https://docs.astral.sh/ruff/rules/regex-flag-alias/#regex-flag-alias-furb167) has `re.match` with an `^` anchor to match the start of the string: ```python if re.match("^hello", "hello world", re.I): ``` But `re.match` already implicitly matches the start of the string: https://docs.python.org/3/library/re.html#search-vs-match Let's change the example to `re.search` so the anchor isn't redundant. (The anchor's actually irrelevant to the example for this rule about long or short flag names.) (Aside: There's a discussion about adding `re.prefixmatch` and [soft] deprecating `re.match` because of the confusion around it: https://discuss.python.org/t/add-re-prefixmatch-deprecate-re-match/105927, python/cpython#86519, python/cpython#31137.) ## Test Plan <!-- How was it tested? --> 1. Create feature branch 2. Push to my fork to run CI 3. Realise feature branches are disabled for forks in Ruff CI 4. Merge feature branch to my `main` 5. Push that 6. Be happy I did, because it failed because I missed something 7. Fixup, pushup 8. Passes [🎉](https://github.com/hugovk/ruff/actions/runs/21524112749)
Resolved conflicts: - Doc/whatsnew/3.14.rst: Used main's version (3.14 is released) - Lib/re/__init__.py: Removed __version__ (removed in main), updated docstring to reference 3.15 instead of 3.14 Added prefixmatch What's New entry to Doc/whatsnew/3.15.rst since the feature is now targeting Python 3.15.
- Change "25 years" to "30 years" to reflect actual time - Replace speculative "this decade, if ever" / "7 years" language with clear statement that we will never remove the original match name Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Fix traceback to include ^ anchor matching the pair pattern definition - Add \A anchor to one example as a teaching hint for readers - Update card game examples to demonstrate search/match/prefixmatch mix - Add explanatory paragraph about match and prefixmatch being identical - Rename compiled regex variables to use _re suffix (valid_hand_re, pair_re) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Using first_name/last_name patterns promotes the myth that names have simple, universal structures. Replace with: - "killer rabbit" with adjective/animal groups - "Norwegian Blue, pining for the fjords" for unlabeled groups
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds
prefixmatchAPIs to the re module as an alternate name for our long existingmatchAPIs to help alleviate a common Python confusion for those coming from other languages regular expression libraries.These alleviate common confusion around what "match" means as Python is different than other popular languages regex libraries in our use of the term as an API name. The original
matchnames are NOT being deprecated. Source tooling like linters, IDEs, and LLMs could suggest usingprefixmatchinstead of match to improve code health and reduce cognitive burden of understanding the intent of code when configured for a modern minimum Python version.See the documentation changes within this PR for a better description.
Documentation Preview: https://cpython-previews--31137.org.readthedocs.build/en/31137/