Skip to content

Conversation

@gpshead
Copy link
Member

@gpshead gpshead commented Feb 5, 2022

Adds prefixmatch APIs to the re module as an alternate name for our long existing match APIs 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 match names are NOT being deprecated. Source tooling like linters, IDEs, and LLMs could suggest using prefixmatch instead 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/

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.
@gpshead gpshead added the type-feature A feature request or enhancement label Feb 5, 2022
@gpshead gpshead marked this pull request as draft January 30, 2023 05:20
@gpshead gpshead changed the title bpo-42353: Add prefixmatch APIs to the re module gh-86519: Add prefixmatch APIs to the re module Jan 30, 2023
@python-cla-bot
Copy link

python-cla-bot bot commented Apr 18, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@gpshead gpshead force-pushed the prefixmatch-b42353 branch 3 times, most recently from 554bb41 to 54c77ca Compare April 19, 2025 01:09
@gpshead gpshead force-pushed the prefixmatch-b42353 branch from 54c77ca to 149f6e4 Compare April 19, 2025 01:20
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)
gpshead and others added 4 commits January 31, 2026 15:18
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
@gpshead gpshead marked this pull request as ready for review February 1, 2026 00:12
@gpshead gpshead requested a review from AA-Turner as a code owner February 1, 2026 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants