-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-98906 re
module: search() vs. match()
section should mention fullmatch()
#98916
Changes from 7 commits
7e90e14
9a1bc61
afab961
f827986
95efa61
84fc8c8
a650b40
1ea3105
d2a6981
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1565,21 +1565,28 @@ search() vs. match() | |
|
||
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> | ||
|
||
Python offers two different primitive operations based on regular expressions: | ||
:func:`re.match` checks for a match only at the beginning of the string, while | ||
:func:`re.search` checks for a match anywhere in the string (this is what Perl | ||
does by default). | ||
Python offers different primitive operations based on regular expressions: | ||
|
||
+ :func:`re.search` checks for a match anywhere in the string, | ||
(this is what Perl does by default) | ||
+ :func:`re.fullmatch` checks for entire string to be a match | ||
+ :func:`re.match` checks for a match only at the beginning of the string | ||
|
||
|
||
For example:: | ||
|
||
>>> re.match("c", "abcdef") # No match | ||
>>> re.search("c", "abcdef") # Match | ||
<re.Match object; span=(2, 3), match='c'> | ||
ericvsmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> re.fullmatch("p.*n", "python") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I missed this: Can you add the "# Match" comment to this line? |
||
<re.Match object; span=(0, 6), match='python'> | ||
ericvsmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> re.fullmatch("r.*n", "python") # No match | ||
|
||
Regular expressions beginning with ``'^'`` can be used with :func:`search` to | ||
restrict the match at the beginning of the string:: | ||
|
||
>>> re.match("c", "abcdef") # No match | ||
>>> re.fullmatch("c", "abcdef") # No Match | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not add this. This section is trying to show the differences between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes sir There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete the |
||
>>> re.search("^c", "abcdef") # No match | ||
>>> re.search("^a", "abcdef") # Match | ||
<re.Match object; span=(0, 1), match='a'> | ||
|
@@ -1588,8 +1595,9 @@ Note however that in :const:`MULTILINE` mode :func:`match` only matches at the | |
beginning of the string, whereas using :func:`search` with a regular expression | ||
beginning with ``'^'`` will match at the beginning of each line. :: | ||
|
||
>>> re.match('X', 'A\nB\nX', re.MULTILINE) # No match | ||
>>> re.search('^X', 'A\nB\nX', re.MULTILINE) # Match | ||
>>> re.match("X", "A\nB\nX", re.MULTILINE) # No match | ||
>>> re.fullmatch("X", "A\nB\nX", re.MULTILINE) # No Match | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And delete this |
||
>>> re.search("^X", "A\nB\nX", re.MULTILINE) # Match | ||
<re.Match object; span=(4, 5), match='X'> | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave these in the order of
match
, thensearch
, thenfullmatch
. That way the match the examples immediately below.