@@ -1471,16 +1471,22 @@ search() vs. match()
1471
1471
1472
1472
.. sectionauthor :: Fred L. Drake, Jr. <fdrake@acm.org>
1473
1473
1474
- Python offers two different primitive operations based on regular expressions:
1475
- :func: `re.match ` checks for a match only at the beginning of the string, while
1476
- :func: `re.search ` checks for a match anywhere in the string (this is what Perl
1477
- does by default).
1474
+ Python offers different primitive operations based on regular expressions:
1475
+
1476
+ + :func: `re.match ` checks for a match only at the beginning of the string
1477
+ + :func: `re.search ` checks for a match anywhere in the string
1478
+ (this is what Perl does by default)
1479
+ + :func: `re.fullmatch ` checks for entire string to be a match
1480
+
1478
1481
1479
1482
For example::
1480
1483
1481
1484
>>> re.match("c", "abcdef") # No match
1482
1485
>>> re.search("c", "abcdef") # Match
1483
1486
<re.Match object; span=(2, 3), match='c'>
1487
+ >>> re.fullmatch("p.*n", "python") # Match
1488
+ <re.Match object; span=(0, 6), match='python'>
1489
+ >>> re.fullmatch("r.*n", "python") # No match
1484
1490
1485
1491
Regular expressions beginning with ``'^' `` can be used with :func: `search ` to
1486
1492
restrict the match at the beginning of the string::
@@ -1494,8 +1500,8 @@ Note however that in :const:`MULTILINE` mode :func:`match` only matches at the
1494
1500
beginning of the string, whereas using :func: `search ` with a regular expression
1495
1501
beginning with ``'^' `` will match at the beginning of each line. ::
1496
1502
1497
- >>> re.match('X', ' A\nB\nX' , re.MULTILINE) # No match
1498
- >>> re.search('^X', ' A\nB\nX' , re.MULTILINE) # Match
1503
+ >>> re.match("X", " A\nB\nX" , re.MULTILINE) # No match
1504
+ >>> re.search("^X", " A\nB\nX" , re.MULTILINE) # Match
1499
1505
<re.Match object; span=(4, 5), match='X'>
1500
1506
1501
1507
0 commit comments