Skip to content

Commit 341972f

Browse files
gpsheadclaude
andcommitted
Address reviewer docs feedback: prefer prefixmatch throughout
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 <noreply@anthropic.com>
1 parent d6090bb commit 341972f

File tree

3 files changed

+28
-42
lines changed

3 files changed

+28
-42
lines changed

Doc/library/re.rst

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ Functions
893893

894894
Compile a regular expression pattern into a :ref:`regular expression object
895895
<re-objects>`, which can be used for matching using its
896-
:func:`~Pattern.prefixmatch` (:func:`~Pattern.match`),
896+
:func:`~Pattern.prefixmatch`,
897897
:func:`~Pattern.search`, and other methods, described below.
898898

899899
The expression's behaviour can be modified by specifying a *flags* value.
@@ -933,16 +933,18 @@ Functions
933933
(the ``|`` operator).
934934

935935

936-
.. function:: match(pattern, string, flags=0)
937936
.. function:: prefixmatch(pattern, string, flags=0)
937+
.. function:: match(pattern, string, flags=0)
938938

939939
If zero or more characters at the beginning of *string* match the regular
940940
expression *pattern*, return a corresponding :class:`~re.Match`. Return
941941
``None`` if the string does not match the pattern; note that this is
942942
different from a zero-length match.
943943

944-
Note that even in :const:`MULTILINE` mode, this will only match at the
945-
beginning of the string and not at the beginning of each line.
944+
.. note::
945+
946+
Even in :const:`MULTILINE` mode, this will only match at the
947+
beginning of the string and not at the beginning of each line.
946948

947949
If you want to locate a match anywhere in *string*, use :func:`search`
948950
instead (see also :ref:`search-vs-match`).
@@ -1284,8 +1286,8 @@ Regular Expression Objects
12841286
>>> pattern.search("dog", 1) # No match; search doesn't include the "d"
12851287

12861288

1287-
.. method:: Pattern.match(string[, pos[, endpos]])
12881289
.. method:: Pattern.prefixmatch(string[, pos[, endpos]])
1290+
.. method:: Pattern.match(string[, pos[, endpos]])
12891291

12901292
If zero or more characters at the *beginning* of *string* match this regular
12911293
expression, return a corresponding :class:`~re.Match`. Return ``None`` if the
@@ -1302,9 +1304,6 @@ Regular Expression Objects
13021304
>>> pattern.prefixmatch("dog") # No match as "o" is not at the start of "dog".
13031305
>>> pattern.prefixmatch("dog", 1) # Match as "o" is the 2nd character of "dog".
13041306
<re.Match object; span=(1, 2), match='o'>
1305-
>>> pattern.match("dog") # Same as above.
1306-
>>> pattern.match("dog", 1) # Same as above.
1307-
<re.Match object; span=(1, 2), match='o'>
13081307

13091308
If you want to locate a match anywhere in *string*, use
13101309
:meth:`~Pattern.search` instead (see also :ref:`search-vs-match`).
@@ -1653,10 +1652,10 @@ That last hand, ``"727ak"``, contained a pair, or two of the same valued cards.
16531652
To match this with a regular expression, one could use backreferences as such::
16541653

16551654
>>> pair_re = re.compile(r".*(.).*\1")
1656-
>>> displaymatch(pair_re.match("717ak")) # Pair of 7s.
1655+
>>> displaymatch(pair_re.prefixmatch("717ak")) # Pair of 7s.
16571656
"<Match: '717', groups=('7',)>"
1658-
>>> displaymatch(pair_re.match("718ak")) # No pairs.
1659-
>>> displaymatch(pair_re.match("354aa")) # Pair of aces.
1657+
>>> displaymatch(pair_re.prefixmatch("718ak")) # No pairs.
1658+
>>> displaymatch(pair_re.prefixmatch("354aa")) # Pair of aces.
16601659
"<Match: '354aa', groups=('a',)>"
16611660

16621661
To find out what card the pair consists of, one could use the
@@ -1676,15 +1675,6 @@ To find out what card the pair consists of, one could use the
16761675
>>> pair_re.prefixmatch("354aa").group(1)
16771676
'a'
16781677

1679-
The examples above use :meth:`~Pattern.match` and :meth:`~Pattern.prefixmatch`
1680-
interchangeably because they are two names for the same method.
1681-
:meth:`~Pattern.prefixmatch` was added in Python 3.15 as a more explicit name;
1682-
use it when your code does not need to run on older Python versions.
1683-
:meth:`~Pattern.search` with a ``^`` or ``\A`` anchor is equivalent, but using
1684-
an explicit method name is clearer to readers of the code.
1685-
See :ref:`prefixmatch-vs-match` for more on this topic.
1686-
1687-
16881678
Simulating scanf()
16891679
^^^^^^^^^^^^^^^^^^
16901680

@@ -1733,22 +1723,20 @@ The equivalent regular expression would be ::
17331723

17341724
.. _search-vs-match:
17351725

1736-
search() vs. match()
1737-
^^^^^^^^^^^^^^^^^^^^
1726+
search() vs. prefixmatch()
1727+
^^^^^^^^^^^^^^^^^^^^^^^^^^
17381728

17391729
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
17401730

17411731
Python offers different primitive operations based on regular expressions:
17421732

1743-
+ :func:`re.prefixmatch`, also known under the less explicit name
1744-
:func:`re.match`, checks for a match only at the beginning of the string
1733+
+ :func:`re.prefixmatch` checks for a match only at the beginning of the string
17451734
+ :func:`re.search` checks for a match anywhere in the string
17461735
(this is what Perl does by default)
17471736
+ :func:`re.fullmatch` checks for entire string to be a match
17481737

17491738
For example::
17501739

1751-
>>> re.match("c", "abcdef") # No match
17521740
>>> re.prefixmatch("c", "abcdef") # No match
17531741
>>> re.search("c", "abcdef") # Match
17541742
<re.Match object; span=(2, 3), match='c'>
@@ -1759,18 +1747,16 @@ For example::
17591747
Regular expressions beginning with ``'^'`` can be used with :func:`search` to
17601748
restrict the match at the beginning of the string::
17611749

1762-
>>> re.match("c", "abcdef") # No match
17631750
>>> re.prefixmatch("c", "abcdef") # No match
17641751
>>> re.search("^c", "abcdef") # No match
17651752
>>> re.search("^a", "abcdef") # Match
17661753
<re.Match object; span=(0, 1), match='a'>
17671754

1768-
Note however that in :const:`MULTILINE` mode :func:`match` only matches at the
1755+
Note however that in :const:`MULTILINE` mode :func:`prefixmatch` only matches at the
17691756
beginning of the string, whereas using :func:`search` with a regular expression
17701757
beginning with ``'^'`` will match at the beginning of each line. ::
17711758

17721759
>>> re.prefixmatch("X", "A\nB\nX", re.MULTILINE) # No match
1773-
>>> re.match("X", "A\nB\nX", re.MULTILINE) # No match
17741760
>>> re.search("^X", "A\nB\nX", re.MULTILINE) # Match
17751761
<re.Match object; span=(4, 5), match='X'>
17761762

@@ -1779,8 +1765,8 @@ beginning with ``'^'`` will match at the beginning of each line. ::
17791765
prefixmatch() vs. match()
17801766
^^^^^^^^^^^^^^^^^^^^^^^^^
17811767

1782-
Why is the :func:`~re.match` function and method name being discouraged in
1783-
favor of the longer :func:`~re.prefixmatch` spelling in very recent Python?
1768+
Why is the :func:`~re.match` function and method discouraged in
1769+
favor of the longer :func:`~re.prefixmatch` spelling?
17841770

17851771
Many other languages have gained regex support libraries since regular
17861772
expressions were added to Python. However in the most popular of those, they
@@ -1796,8 +1782,8 @@ understand the intended semantics. When reading :func:`~re.match` there remains
17961782
a seed of doubt about the intended behavior to anyone not already familiar with
17971783
this old Python gotcha.
17981784

1799-
We will **never** remove the original :func:`~re.match` name, as it has been
1800-
used in code for over 30 years.
1785+
We **do not** plan to deprecate and remove the older *match* name,
1786+
if ever, as it has been used in code for over 30 years.
18011787

18021788
.. versionadded:: next
18031789

Doc/whatsnew/3.15.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,6 @@ os.path
694694
(Contributed by Petr Viktorin for :cve:`2025-4517`.)
695695

696696

697-
resource
698-
--------
699-
700-
* Add new constants: :data:`~resource.RLIMIT_NTHR`,
701-
:data:`~resource.RLIMIT_UMTXP`, :data:`~resource.RLIMIT_THREADS`,
702-
:data:`~resource.RLIM_SAVED_CUR`, and :data:`~resource.RLIM_SAVED_MAX`.
703-
(Contributed by Serhiy Storchaka in :gh:`137512`.)
704-
705-
706697
re
707698
--
708699

@@ -716,6 +707,15 @@ re
716707
(Contributed by Gregory P. Smith in :gh:`86519`.)
717708

718709

710+
resource
711+
--------
712+
713+
* Add new constants: :data:`~resource.RLIMIT_NTHR`,
714+
:data:`~resource.RLIMIT_UMTXP`, :data:`~resource.RLIMIT_THREADS`,
715+
:data:`~resource.RLIM_SAVED_CUR`, and :data:`~resource.RLIM_SAVED_MAX`.
716+
(Contributed by Serhiy Storchaka in :gh:`137512`.)
717+
718+
719719
shelve
720720
------
721721

Lib/re/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def prefixmatch(pattern, string, flags=0):
165165
a Match object, or None if no match was found."""
166166
return _compile(pattern, flags).prefixmatch(string)
167167

168-
# Our original less explicitly clear about the behavior name for prefixmatch.
168+
# Our original name which was less explicitly clear about the behavior for prefixmatch.
169169
match = prefixmatch
170170

171171
def fullmatch(pattern, string, flags=0):

0 commit comments

Comments
 (0)