@@ -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.
16531652To 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
16621661To 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-
16881678Simulating 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
17411731Python 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
17491738For 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::
17591747Regular expressions beginning with ``'^' `` can be used with :func: `search ` to
17601748restrict 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
17691756beginning of the string, whereas using :func: `search ` with a regular expression
17701757beginning 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. ::
17791765prefixmatch() 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
17851771Many other languages have gained regex support libraries since regular
17861772expressions 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
17961782a seed of doubt about the intended behavior to anyone not already familiar with
17971783this 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
0 commit comments