Skip to content

Commit ff22eb2

Browse files
committed
Revise 'prefixmatch() versus search()'
1 parent 4090e69 commit ff22eb2

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

Doc/howto/regex.rst

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,37 +1281,32 @@ can be solved with a faster and simpler string method.
12811281
prefixmatch() versus search()
12821282
-----------------------------
12831283

1284-
The :func:`~re.prefixmatch` function only checks if the RE matches at the beginning of the
1285-
string while :func:`~re.search` will scan forward through the string for a match.
1286-
It's important to keep this distinction in mind. Remember, :func:`!prefixmatch` will
1287-
only report a successful match which will start at 0; if the match wouldn't
1288-
start at zero, :func:`!prefixmatch` will *not* report it. ::
1284+
:func:`~re.prefixmatch` was added in Python 3.15 as the :ref:`preferred name
1285+
<prefixmatch-vs-match>` for :func:`~re.match`. Before this, it was only known
1286+
as :func:`!match` and the distinction with :func:`~re.search` was often
1287+
misunderstood.
1288+
1289+
:func:`!prefixmatch` aka :func:`!match` only checks if the RE matches at the
1290+
beginning of the string while :func:`!search` scans forward through the
1291+
string for a match. :func:`!prefixmatch` only reports a successful match which
1292+
starts at zero; if the match wouldn't start at zero, :func:`!prefixmatch` will
1293+
*not* report it. ::
12891294

12901295
>>> print(re.prefixmatch('super', 'superstition').span())
12911296
(0, 5)
12921297
>>> print(re.prefixmatch('super', 'insuperable'))
12931298
None
12941299

1295-
On the other hand, :func:`~re.search` will scan forward through the string,
1300+
On the other hand, :func:`~re.search` scans forward through the string,
12961301
reporting the first match it finds. ::
12971302

12981303
>>> print(re.search('super', 'superstition').span())
12991304
(0, 5)
13001305
>>> print(re.search('super', 'insuperable').span())
13011306
(2, 7)
13021307

1303-
Sometimes you'll be tempted to keep using :func:`re.prefixmatch`, and just add ``.*``
1304-
to the front of your RE. Resist this temptation and use :func:`re.search`
1305-
instead. The regular expression compiler does some analysis of REs in order to
1306-
speed up the process of looking for a match. One such analysis figures out what
1307-
the first character of a match must be; for example, a pattern starting with
1308-
``Crow`` must match starting with a ``'C'``. The analysis lets the engine
1309-
quickly scan through the string looking for the starting character, only trying
1310-
the full match if a ``'C'`` is found.
1311-
1312-
Adding ``.*`` defeats this optimization, requiring scanning to the end of the
1313-
string and then backtracking to find a match for the rest of the RE. Use
1314-
:func:`re.search` instead.
1308+
This distinction is important to remember when using the old :func:`~re.match`
1309+
name in code requiring compatibility with older Python versions.
13151310

13161311

13171312
Greedy versus Non-Greedy

0 commit comments

Comments
 (0)