Repository navigation
Proposal: re.prefixmatch method (alias for re.match) #86519
Description
Activity
A well known anti-pattern in Python is use of re.match when you meant to use re.search.
re.fullmatchwas added in 3.4 via #60407 for similar reasons.re.prefixmatchwould be similar: we want the re.match behavior, but want the code to be obvious about its intent. This documents the implicit\A(or^when notre.MULTILINE) in the name.The goal would be to allow linters to ultimately flag
re.matchas the anti-pattern when in 3.12+ mode. Asking people to usere.prefixmatchorre.searchinstead.This would help avoid bugs where people mean
re.searchbut writere.matchand would ease the cognitive load at code review time by encouraging explicit code without detailed specific Pythonredomain knowledge on the reviewers part.The implementation is trivial.
This is not a decision to deprecate the widely used in 30 years worth of code's re.match name. That'd be painful and is unlikely to be worth doing.
Discussion in https://discuss.python.org/t/add-re-prefixmatch-deprecate-re-match/105927
Reacted by Oleg Iarygin- added3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
on Nov 13, 2020 I seen a code which uses re.search() with anchor ^ instead of re.match(), but I never seen a code which uses re.match() instead of re.search(). It just won't work unless you add explicit ".*" or ".*?" at the start of the pattern, and it is a clear indication that re.match() matches the start of the string.
My point is that re.match is a common bug when people really want re.search.
re.prefixmatch makes it explicit and non-confusing and thus unlikely to be used wrong or misunderstood when read or reviewed.
The term "match" when talking about regular expressions is not normally meant to imply any anchoring as anchors can be expressed within the regex. Python is relatively unique in bothering to have different methods for a prefix match and an anywhere match. (We'd have been better off without a match method entirely, only having search - too late now)
It just won't work unless you add explicit ".*" or ".*?" at the start of the pattern
But think of when regexes are used for validating input. Getting it to "just work" may be over-permissive validation that only actually checks the beginning of the input. They're one missed test case away from a crash or, worse, a security issue.
This proposed name change would help make the function behavior obvious at the callsite. In the validator example, calling "prefixmatch" would stand out as wrong to even the most oblivious, documentation-averse user.
My point is that re.match is a common bug when people really want re.search.
While I think better distinguishing the interfaces is a nice-to-have for usability, I think it has more absolute benefit to correctness. Again, confusion around the semantics of "match" were the motivation for adding "fullmatch" in the first place but that change only went so far to address the problem: It's still too easy to misuse the existing "match" interface and it's not realistic to remove it from the language. A new name would eliminate this class of error at a very low cost.
Reacted by Gregory P. Smith- added3.11only security fixesonly security fixesand removed3.10 (EOL)end of lifeend of life
on Feb 4, 2022 14 remaining items
There is a good case to be made that we as a project should provide automated code transformers with every Python release that can be used to migrate source code towards the preferred more modern thing when feasible (and encourage changes to be such that this is feasible). Past examples:
2to3did this where it could (limited) and ultimately is what lies behind themodernizetool which kept that torch lit and is more useful.But I wouldn't want to block this mere preferred more explicit name alias going in until such tooling to handle a decade later potential deprecation of the old name being available. They're separate requests.
Reacted by Ezio Melotti and Hugo van Kemenade- added3.13only security fixesonly security fixesand removed3.12only security fixesonly security fixes
on Jan 5, 2024 +1 - adding "prefixmatch" is a clear win, with scant downside, provided "match" doesn't go away.
Reacted by Gregory P. Smith, Shantanu and Hugo van Kemenade- added a commit that references this issue
on Jan 30, 2026 - added a commit that references this issue
on Feb 16, 2026 merged, thanks for everyones reviews and discussions!
Reacted by Hugo van KemenadeUpdating
regexis tracked in mrabarnett/mrab-regex#601- added a commit that references this issue
on Apr 14, 2026 - added a commit that references this issue
on May 1, 2026
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
prefixmatch#148096