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