Skip to content
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

Merged
merged 9 commits into from
Nov 30, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Doc/library/re.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1565,18 +1565,22 @@ search() vs. match()
Python offers two different primitive operations based on regular expressions:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe continue the style of this paragraph, so instead something like 'Python offers a few primitive ...' then '... re.fullmatch checks that the whole string is a match ...' (can change this wording up a bit too). I also think the None case can omitted since it wasn't mentioned in the previous two cases either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done sir

: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).
does by default).We also have :func:`re.fullmatch` which checks whether the complete
string is a match, otherwise it returns None.

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("python", "python") # Match
<re.Match object; span=(0, 6), match='python'>
ericvsmith marked this conversation as resolved.
Show resolved Hide resolved

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
Copy link
Member

Choose a reason for hiding this comment

The 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 search and match. Adding a fullmatch example just confuses things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sir

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete the fullmatch line here.

>>> re.search("^c", "abcdef") # No match
>>> re.search("^a", "abcdef") # Match
<re.Match object; span=(0, 1), match='a'>
Expand All @@ -1586,6 +1590,7 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, other examples seem to use double quotes so this and re.search can be swapped over, or re.fullmatch can be changed to single quote (but optional).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done the changes

>>> re.fullmatch("X", "A\nB\nX", re.MULTILINE) # No Match
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And delete thisfullmatch line also. This section doesn't need to mention fullmatch.

>>> re.search('^X', 'A\nB\nX', re.MULTILINE) # Match
<re.Match object; span=(4, 5), match='X'>

Expand Down