@@ -1446,7 +1446,7 @@ when there is no match, you can test whether there was a match with a simple
14461446 If a group is contained in a part of the pattern that matched multiple times,
14471447 the last match is returned. ::
14481448
1449- >>> m = re.search(r"(\w+) (\w+)", "Isaac Newton, physicist")
1449+ >>> m = re.search(r"\A (\w+) (\w+)", "Isaac Newton, physicist")
14501450 >>> m.group(0) # The entire match
14511451 'Isaac Newton'
14521452 >>> m.group(1) # The first parenthesized subgroup.
@@ -1641,41 +1641,49 @@ representing the card with that value.
16411641
16421642To see if a given string is a valid hand, one could do the following::
16431643
1644- >>> valid = re.compile(r"^[a2-9tjqk]{5}$")
1645- >>> displaymatch(valid .search("akt5q")) # Valid.
1644+ >>> valid_hand_re = re.compile(r"^[a2-9tjqk]{5}$")
1645+ >>> displaymatch(valid_hand_re .search("akt5q")) # Valid.
16461646 "<Match: 'akt5q', groups=()>"
1647- >>> displaymatch(valid .search("akt5e")) # Invalid.
1648- >>> displaymatch(valid .search("akt")) # Invalid.
1649- >>> displaymatch(valid .search("727ak")) # Valid.
1647+ >>> displaymatch(valid_hand_re .search("akt5e")) # Invalid.
1648+ >>> displaymatch(valid_hand_re .search("akt")) # Invalid.
1649+ >>> displaymatch(valid_hand_re .search("727ak")) # Valid.
16501650 "<Match: '727ak', groups=()>"
16511651
16521652That last hand, ``"727ak" ``, contained a pair, or two of the same valued cards.
16531653To match this with a regular expression, one could use backreferences as such::
16541654
1655- >>> pair = re.compile(r"^ .*(.).*\1")
1656- >>> displaymatch(pair.search ("717ak")) # Pair of 7s.
1655+ >>> pair_re = re.compile(r".*(.).*\1")
1656+ >>> displaymatch(pair_re.match ("717ak")) # Pair of 7s.
16571657 "<Match: '717', groups=('7',)>"
1658- >>> displaymatch(pair.search ("718ak")) # No pairs.
1659- >>> displaymatch(pair.search ("354aa")) # Pair of aces.
1658+ >>> displaymatch(pair_re.match ("718ak")) # No pairs.
1659+ >>> displaymatch(pair_re.match ("354aa")) # Pair of aces.
16601660 "<Match: '354aa', groups=('a',)>"
16611661
16621662To find out what card the pair consists of, one could use the
16631663:meth: `~Match.group ` method of the match object in the following manner::
16641664
1665- >>> pair = re.compile(r"^ .*(.).*\1")
1666- >>> pair.search ("717ak").group(1)
1665+ >>> pair_re = re.compile(r".*(.).*\1")
1666+ >>> pair_re.prefixmatch ("717ak").group(1)
16671667 '7'
16681668
1669- # Error because re.search () returns None, which doesn't have a group() method:
1670- >>> pair.search ("718ak").group(1)
1669+ # Error because prefixmatch () returns None, which doesn't have a group() method:
1670+ >>> pair_re.prefixmatch ("718ak").group(1)
16711671 Traceback (most recent call last):
16721672 File "<pyshell#23>", line 1, in <module>
1673- re.search(r".*(.).*\1", "718ak").group(1)
1673+ pair_re.prefixmatch( "718ak").group(1)
16741674 AttributeError: 'NoneType' object has no attribute 'group'
16751675
1676- >>> pair.search ("354aa").group(1)
1676+ >>> pair_re.prefixmatch ("354aa").group(1)
16771677 'a'
16781678
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+
16791687
16801688Simulating scanf()
16811689^^^^^^^^^^^^^^^^^^
0 commit comments