@@ -1565,16 +1565,22 @@ search() vs. match()
15651565
15661566..
sectionauthor ::
Fred L. Drake, Jr. <[email protected] > 15671567
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+
15721575
15731576For example::
15741577
15751578 >>> re.match("c", "abcdef") # No match
15761579 >>> re.search("c", "abcdef") # Match
15771580 <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
15781584
15791585Regular expressions beginning with ``'^' `` can be used with :func: `search ` to
15801586restrict 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
15881594beginning of the string, whereas using :func: `search ` with a regular expression
15891595beginning with ``'^' `` will match at the beginning of each line. ::
15901596
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
15931599 <re.Match object; span=(4, 5), match='X'>
15941600
15951601
0 commit comments