Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 443f000

Browse files
committed
#14155: remove duplication about search vs match in re doc.
1 parent 5a045b9 commit 443f000

1 file changed

Lines changed: 28 additions & 52 deletions

File tree

Doc/library/re.rst

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -423,31 +423,6 @@ a group reference. As for string literals, octal escapes are always at most
423423
three digits in length.
424424

425425

426-
.. _matching-searching:
427-
428-
Matching vs. Searching
429-
----------------------
430-
431-
.. sectionauthor:: Fred L. Drake, Jr. <[email protected]>
432-
433-
434-
Python offers two different primitive operations based on regular expressions:
435-
**match** checks for a match only at the beginning of the string, while
436-
**search** checks for a match anywhere in the string (this is what Perl does
437-
by default).
438-
439-
Note that match may differ from search even when using a regular expression
440-
beginning with ``'^'``: ``'^'`` matches only at the start of the string, or in
441-
:const:`MULTILINE` mode also immediately following a newline. The "match"
442-
operation succeeds only if the pattern matches at the start of the string
443-
regardless of mode, or at the starting position given by the optional *pos*
444-
argument regardless of whether a newline precedes it.
445-
446-
>>> re.match("c", "abcdef") # No match
447-
>>> re.search("c", "abcdef") # Match
448-
<_sre.SRE_Match object at ...>
449-
450-
451426
.. _contents-of-module-re:
452427

453428
Module Contents
@@ -581,10 +556,11 @@ form.
581556
<match-objects>`. Return ``None`` if the string does not match the pattern;
582557
note that this is different from a zero-length match.
583558

584-
.. note::
559+
Note that even in :const:`MULTILINE` mode, :func:`re.match` will only match
560+
at the beginning of the string and not at the beginning of each line.
585561

586-
If you want to locate a match anywhere in *string*, use :func:`search`
587-
instead.
562+
If you want to locate a match anywhere in *string*, use :func:`search`
563+
instead (see also :ref:`search-vs-match`).
588564

589565

590566
.. function:: split(pattern, string, maxsplit=0, flags=0)
@@ -768,16 +744,14 @@ attributes:
768744
The optional *pos* and *endpos* parameters have the same meaning as for the
769745
:meth:`~regex.search` method.
770746

771-
.. note::
772-
773-
If you want to locate a match anywhere in *string*, use
774-
:meth:`~regex.search` instead.
775-
776747
>>> pattern = re.compile("o")
777748
>>> pattern.match("dog") # No match as "o" is not at the start of "dog".
778749
>>> pattern.match("dog", 1) # Match as "o" is the 2nd character of "dog".
779750
<_sre.SRE_Match object at ...>
780751

752+
If you want to locate a match anywhere in *string*, use
753+
:meth:`~regex.search` instead (see also :ref:`search-vs-match`).
754+
781755

782756
.. method:: regex.split(string, maxsplit=0)
783757

@@ -1139,37 +1113,39 @@ the above regular expression can avoid recursion by being recast as ``Begin
11391113
[a-zA-Z0-9_ ]*?end``. As a further benefit, such regular expressions will run
11401114
faster than their recursive equivalents.
11411115

1116+
.. _search-vs-match:
11421117

11431118
search() vs. match()
11441119
^^^^^^^^^^^^^^^^^^^^
11451120

1146-
In a nutshell, :func:`match` only attempts to match a pattern at the beginning
1147-
of a string where :func:`search` will match a pattern anywhere in a string.
1148-
For example:
1121+
.. sectionauthor:: Fred L. Drake, Jr. <[email protected]>
11491122

1150-
>>> re.match("o", "dog") # No match as "o" is not the first letter of "dog".
1151-
>>> re.search("o", "dog") # Match as search() looks everywhere in the string.
1152-
<_sre.SRE_Match object at ...>
1123+
Python offers two different primitive operations based on regular expressions:
1124+
:func:`re.match` checks for a match only at the beginning of the string, while
1125+
:func:`re.search` checks for a match anywhere in the string (this is what Perl
1126+
does by default).
11531127

1154-
.. note::
1128+
For example::
11551129

1156-
The following applies only to regular expression objects like those created
1157-
with ``re.compile("pattern")``, not the primitives ``re.match(pattern,
1158-
string)`` or ``re.search(pattern, string)``.
1130+
>>> re.match("c", "abcdef") # No match
1131+
>>> re.search("c", "abcdef") # Match
1132+
<_sre.SRE_Match object at ...>
11591133

1160-
:func:`match` has an optional second parameter that gives an index in the string
1161-
where the search is to start::
1134+
Regular expressions beginning with ``'^'`` can be used with :func:`search` to
1135+
restrict the match at the beginning of the string::
11621136

1163-
>>> pattern = re.compile("o")
1164-
>>> pattern.match("dog") # No match as "o" is not at the start of "dog."
1137+
>>> re.match("c", "abcdef") # No match
1138+
>>> re.search("^c", "abcdef") # No match
1139+
>>> re.search("^a", "abcdef") # Match
1140+
<_sre.SRE_Match object at ...>
11651141

1166-
# Equivalent to the above expression as 0 is the default starting index:
1167-
>>> pattern.match("dog", 0)
1142+
Note however that in :const:`MULTILINE` mode :func:`match` only matches at the
1143+
beginning of the string, whereas using :func:`search` with a regular expression
1144+
beginning with ``'^'`` will match at the beginning of each line.
11681145

1169-
# Match as "o" is the 2nd character of "dog" (index 0 is the first):
1170-
>>> pattern.match("dog", 1)
1146+
>>> re.match('X', 'A\nB\nX', re.MULTILINE) # No match
1147+
>>> re.search('^X', 'A\nB\nX', re.MULTILINE) # Match
11711148
<_sre.SRE_Match object at ...>
1172-
>>> pattern.match("dog", 2) # No match as "o" is not the 3rd character of "dog."
11731149

11741150

11751151
Making a Phonebook

0 commit comments

Comments
 (0)