@@ -1565,16 +1565,22 @@ search() vs. match()
1565
1565
1566
1566
.. sectionauthor :: Fred L. Drake, Jr. <fdrake@acm.org>
1567
1567
1568
- Python offers two different primitive operations based on regular expressions:
1569
- :func: `re.match ` checks for a match only at the beginning of the string, while
1570
- :func: `re.search ` checks for a match anywhere in the string (this is what Perl
1571
- does by default).
1568
+ Python offers different primitive operations based on regular expressions:
1569
+
1570
+ + :func: `re.match ` checks for a match only at the beginning of the string
1571
+ + :func: `re.search ` checks for a match anywhere in the string
1572
+ (this is what Perl does by default)
1573
+ + :func: `re.fullmatch ` checks for entire string to be a match
1574
+
1572
1575
1573
1576
For example::
1574
1577
1575
1578
>>> re.match("c", "abcdef") # No match
1576
1579
>>> re.search("c", "abcdef") # Match
1577
1580
<re.Match object; span=(2, 3), match='c'>
1581
+ >>> re.fullmatch("p.*n", "python") # Match
1582
+ <re.Match object; span=(0, 6), match='python'>
1583
+ >>> re.fullmatch("r.*n", "python") # No match
1578
1584
1579
1585
Regular expressions beginning with ``'^' `` can be used with :func: `search ` to
1580
1586
restrict the match at the beginning of the string::
@@ -1588,8 +1594,8 @@ Note however that in :const:`MULTILINE` mode :func:`match` only matches at the
1588
1594
beginning of the string, whereas using :func: `search ` with a regular expression
1589
1595
beginning with ``'^' `` will match at the beginning of each line. ::
1590
1596
1591
- >>> re.match('X', ' A\nB\nX' , re.MULTILINE) # No match
1592
- >>> re.search('^X', ' A\nB\nX' , re.MULTILINE) # Match
1597
+ >>> re.match("X", " A\nB\nX" , re.MULTILINE) # No match
1598
+ >>> re.search("^X", " A\nB\nX" , re.MULTILINE) # Match
1593
1599
<re.Match object; span=(4, 5), match='X'>
1594
1600
1595
1601
0 commit comments