GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-86519: Add prefixmatch APIs to the re module by gpshead · Pull Request #31137 · python/cpython

Conversation

@gpshead

@gpshead gpshead commented Feb 5, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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 for a better description.

Discussions took place in the PR, in the issue, and finally at https://discuss.python.org/t/add-re-prefixmatch-deprecate-re-match/105927

  • Issue: Proposal: re.prefixmatch method (alias for re.match) #86519

Documentation Preview: https://cpython-previews--31137.org.readthedocs.build/en/31137/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hauntsaninja and hugovk reacted with thumbs up emoji msuozzo reacted with hooray emoji
All reactions
  • 👍 2 reactions
  • 🎉 1 reaction
@gpshead
bpo-42353: Add prefixmatch APIs to the re module.
587dc4d
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
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Feb 5, 2022
@bedevere-bot bedevere-bot added the awaiting core review label Feb 5, 2022
@gpshead gpshead added the DO-NOT-MERGE label Feb 17, 2022
@gpshead gpshead mentioned this pull request Jul 7, 2022
Proposal: re.prefixmatch method (alias for re.match) #86519
Closed
@ezio-melotti ezio-melotti removed the CLA signed label Jul 13, 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
gpshead added 3 commits January 29, 2023 22:45
@gpshead
Merge branch 'main' into prefixmatch-b42353
c7d1581
@gpshead
fix editor fat finger typo during the merge. #vim
fa2885e
@gpshead
3.12
6195ad7
@gpshead gpshead removed the DO-NOT-MERGE label Jan 30, 2023
@hauntsaninja
Merge branch 'main' into prefixmatch-b42353
0296f29
@python-cla-bot

python-cla-bot Bot commented Apr 18, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead
Merge main and modernize the relevant docs.
bab8e8b
@gpshead
gpshead force-pushed the prefixmatch-b42353 branch 3 times, most recently from 554bb41 to 54c77ca Compare April 19, 2025 01:09
@gpshead
Simplify, don't have duplicate definitions.
149f6e4
`match = prefixmatch`
@gpshead
gpshead force-pushed the prefixmatch-b42353 branch from 54c77ca to 149f6e4 Compare April 19, 2025 01:20
@hugovk hugovk mentioned this pull request Jan 30, 2026
FURB167: change example to re.search with ^ anchor astral-sh/ruff#22984
Merged
ntBre pushed a commit to astral-sh/ruff that referenced this pull request Jan 30, 2026
@hugovk
FURB167: change example to re.search with ^ anchor (#22984)
38d4f49
## 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
@gpshead
Merge main into prefixmatch-b42353
e176692
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.
@gpshead @claude
Update match name removal language to be definitive
58d8f57
- 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 <[email protected]>
@gpshead @claude
Fix re.rst examples after match to search conversion
3cd9d71
- 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 <[email protected]>
@gpshead
Replace name-parsing examples with Monty Python references
d6090bb
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
hauntsaninja
hauntsaninja approved these changes Feb 8, 2026
View reviewed changes

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you for making this happen!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_sre/sre.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Feb 8, 2026
hauntsaninja
hauntsaninja reviewed Feb 8, 2026
View reviewed changes
Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Feb 8, 2026

Copy link
Copy Markdown
Member

Did we actually reached a consensus on DPO for that feature to be merged? I thought there were some issues, especially with the fact that regex needs to be updated to be with parity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz reviewed Feb 8, 2026
View reviewed changes
Comment thread Lib/test/test_re.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@hugovk

hugovk commented Feb 8, 2026

Copy link
Copy Markdown
Member

Did we actually reached a consensus on DPO for that feature to be merged?

Yes, I think the 24 hearts on Greg's https://discuss.python.org/t/add-re-prefixmatch-deprecate-re-match/105927/20 is a pretty good sign.

The only other posts with double-digit hearts are also in support: the OP (10), a docs suggestion from Tim (14), a name suggestion (startswith) from Marc-André (11).

I thought there were some issues, especially with the fact that regex needs to be updated to be with parity.

Whatever the third-party regex package does or doesn't do, shouldn't hold us back. They say "This regex implementation is backwards-compatible with the standard ‘re’ module, but offers additional functionality", so it doesn't sound like it'll be a problem for them to do the same thing.

gpshead reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hugovk
hugovk approved these changes Feb 8, 2026
View reviewed changes

@hugovk hugovk left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Non-blocking docs suggestion:

Use prefixmatch pretty much throughout, except to mention match is the alias (plus version changed etc.), which you've mostly already done here.

We can also update other places like the regex HOWTO to prefer the new name, but this can also be in a followup.

Bike shed colour:

I'm fine with either prefixmatch and startswith.

Pro for prefixmatch: it keeps the "match" part, which might help remind people when they see match: "oh, wait, is this the lopsided prefixmatch or fullmatch? I'd better double check the docs and use one of those descriptive names or instead search with explicit anchors."

Soft deprecation:

We're discouraging in docs and not planning on removing. Sounds like soft deprecation. I'd be fine with soft deprecating too, but that could also be another discussion once this is done (if anyone has energy for that!).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/re.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/whatsnew/3.15.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/re/__init__.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
picnixz requested a review from serhiy-storchaka February 8, 2026 21:59
@gpshead

gpshead commented Feb 8, 2026

Copy link
Copy Markdown
Member Author

Bike shed colour:
I'm fine with either prefixmatch and startswith.
Pro for prefixmatch: it keeps the "match" part, which might help remind people

I'm similarly inclined to stick with prefixmatch. It ties in with match wording elsewhere and avoids confusion w.r.t. the str/bytes methods of the same startswith name.

lots of what look like good comments on the docs here and thoughts on how to document this to digest from the discuss thread. i'll work on getting the doc edits in and see if it looks like it needs more review after that.

hauntsaninja and hugovk reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gpshead and others added 8 commits February 9, 2026 00:05
@gpshead @claude
Address reviewer docs feedback: prefer prefixmatch throughout
341972f
Apply suggestions from hugovk and hauntsaninja reviews:
- List prefixmatch before match in function/method signatures
- Use prefixmatch exclusively in examples, remove redundant match duplicates
- Remove interchangeable paragraph (covered by prefixmatch-vs-match section)
- Rename section to "search() vs. prefixmatch()"
- Add .. note:: directive for MULTILINE caveat
- Fix time-sensitive wording ("very recent Python", "never")
- Fix alphabetical ordering in whatsnew/3.15.rst
- Fix comment grammar in Lib/re/__init__.py

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@gpshead @claude
Statically alias match to prefixmatch instead of runtime memcpy
cd8d85d
Replace the copy_prefixmatch_method_def_to_match() runtime memcpy with
static struct initializers that reuse the Clinic-generated prefixmatch
parser directly. This avoids duplicating argument parsing boilerplate
while keeping everything initialized at compile time.

Add Py_DEBUG assertions in sre_exec to verify the match and prefixmatch
method table entries remain identical except for ml_name.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@gpshead
Merge main into prefixmatch-b42353
74d51b3
@gpshead @claude
Fix whatsnew 3.15 RST heading that matches git conflict marker
4abdff0
The "Removed" heading underline was exactly 7 '=' characters, which
triggers the check-merge-conflict pre-commit hook as a false positive.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@gpshead
docs, remove errant "if ever"
6bacfb0
@gpshead
doc wording tweaks jf clarity
40e6a2a
@gpshead @claude
Extract do_test_match_getitem helper to preserve git blame
b3c6948
Refactor test_match_getitem to use a helper method instead of a
for loop, keeping the assertion lines at their original indentation
to preserve git line attribution history.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@gpshead
tweak news to match docs
b277981
@gpshead gpshead self-assigned this Feb 9, 2026
@gpshead
gpshead merged commit 300de1e into python:main Feb 16, 2026
50 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 16, 2026
@gpshead

gpshead commented Feb 16, 2026

Copy link
Copy Markdown
Member Author

merged, thanks for everyones reviews and discussions!

hugovk reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead gpshead mentioned this pull request Feb 16, 2026
Add an explicit prefixmatch API name for match for parity CPython 3.15 mrabarnett/mrab-regex#601
Closed
brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
@gpshead @claude
@hauntsaninja @brijkapadia
pythongh-86519: Add prefixmatch APIs to the re module (pythonGH-31137)
22df6fe
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 for a better description.

Discussions took place in the PR, in the issue, and finally at https://discuss.python.org/t/add-re-prefixmatch-deprecate-re-match/105927

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Co-authored-by: Shantanu <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
@vstinner vstinner mentioned this pull request Mar 18, 2026
Update test_validators.py for Python 3.15a7 python-attrs/attrs#1530
Merged
3 of 12 tasks
@hugovk

hugovk commented Apr 4, 2026

Copy link
Copy Markdown
Member

We can also update other places like the regex HOWTO to prefer the new name, but this can also be in a followup.

Please see PR #148096.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hugovk hugovk mentioned this pull request Apr 4, 2026
Soft deprecate re.match and re.Pattern.match in favour of prefixmatch #148100
Closed
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@gpshead @claude
@hauntsaninja @ljfp
pythongh-86519: Add prefixmatch APIs to the re module (pythonGH-31137)
a8c1cbc
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 for a better description.

Discussions took place in the PR, in the issue, and finally at https://discuss.python.org/t/add-re-prefixmatch-deprecate-re-match/105927

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Co-authored-by: Shantanu <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
KotlinIsland pushed a commit to KotlinIsland/basedpython that referenced this pull request May 1, 2026
@hugovk
FURB167: change example to re.search with ^ anchor (#22984)
730e469
## 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)
@samhaese samhaese mentioned this pull request Jun 2, 2026
(skip issue) Respect Isaac Newton and Malcolm Reynolds by reverting their removal in #31137 #150807
Closed
decorator-factory added a commit to decorator-factory/python-re-quiz that referenced this pull request Aug 8, 2026
@decorator-factory
Acknowledge the match -> prefixmatch change
9d8f49d
See python/cpython#31137 for more details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@picnixz picnixz picnixz left review comments
@hauntsaninja hauntsaninja hauntsaninja approved these changes
@hugovk hugovk hugovk approved these changes
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@serhiy-storchaka serhiy-storchaka Awaiting requested review from serhiy-storchaka

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@gpshead gpshead

Labels

type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@gpshead @picnixz @hugovk @hauntsaninja @the-knights-who-say-ni @ezio-melotti @bedevere-bot
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.