Skip to content
Merged
Changes from 6 commits
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
21 changes: 14 additions & 7 deletions Doc/library/re.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1565,21 +1565,27 @@ 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
Copy link
Copy Markdown
Member

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, then search, then fullmatch. That way the match the examples immediately below.



For example::

>>> re.match("c", "abcdef") # No match
>>> re.search("c", "abcdef") # Match
<re.Match object; span=(2, 3), match='c'>
Comment thread
ericvsmith marked this conversation as resolved.
>>> re.match("c", "cdef") # match
<re.Match object; span=(0, 1), match='c'>
>>> re.search("c", "cdef") # Match
<re.Match object; span=(0, 1), match='c'>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And I would not add these 2 examples. The existing examples are fine. The goal is to show that search() can match something that match() does not, with the same parameters to both.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes sir

>>> re.fullmatch("c", "cdef") # No Match
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry I wasn't clear. I think you need two examples for fullmatch:

re.fullmatch("p.*n", "python")  # Match
re.fullmatch("r.*n", "python") # No match

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done sir


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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 @@ -1589,6 +1595,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
Copy Markdown
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
Copy Markdown
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
>>> re.search('^X', 'A\nB\nX', re.MULTILINE) # Match
<re.Match object; span=(4, 5), match='X'>

Expand Down