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

Conversation

ramvikrams
Copy link
Contributor

@ramvikrams ramvikrams commented Oct 31, 2022

Added the re.fullmatch() with it's examples

GH-98906 re module: search() vs. match() section should mention fullmatch()

adding re.fullmatch with it's examples
@@ -1565,18 +1565,22 @@ search() vs. match()
Python offers two different primitive operations based on regular expressions:
Copy link
Contributor

@slateny slateny Nov 1, 2022

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

@ramvikrams ramvikrams Nov 1, 2022

Choose a reason for hiding this comment

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

Done sir

@@ -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

@slateny slateny Nov 1, 2022

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

@ramvikrams ramvikrams Nov 1, 2022

Choose a reason for hiding this comment

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

Done the changes

:func:`re.fullmatch` checks for entire string to be a match. :func:`re.search`
checks for a match anywhere in the string (this is what Perl does by default).
Copy link
Contributor

@slateny slateny Nov 1, 2022

Choose a reason for hiding this comment

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

For better flow

Suggested change
:func:`re.fullmatch` checks for entire string to be a match. :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, and
:func:`re.search` checks for a match anywhere in the string
(this is what Perl does by default).

or perhaps even

Suggested change
:func:`re.fullmatch` checks for entire string to be a match. :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, and
:func:`re.search` checks for a match anywhere in the string.

but I'll let Eric decide whether the Perl mention is necessary.

Copy link
Contributor Author

@ramvikrams ramvikrams Nov 1, 2022

Choose a reason for hiding this comment

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

ok

Copy link
Contributor

@hauntsaninja hauntsaninja Nov 1, 2022

Choose a reason for hiding this comment

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

My recommendation would be to use bullet points (and to reorder so that plain re.match is last, honestly it's a little bit of a footgun)

* :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

@slateny slateny requested a review from ericvsmith Nov 1, 2022
:func:`re.fullmatch` checks for entire string to be a match. :func:`re.search`
checks for a match anywhere in the string (this is what Perl does by default).
Copy link
Contributor

@hauntsaninja hauntsaninja Nov 1, 2022

Choose a reason for hiding this comment

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

My recommendation would be to use bullet points (and to reorder so that plain re.match is last, honestly it's a little bit of a footgun)

* :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

Doc/library/re.rst Show resolved Hide resolved
@ramvikrams ramvikrams closed this Nov 30, 2022
@ramvikrams ramvikrams deleted the t2 branch Nov 30, 2022
@ramvikrams ramvikrams restored the t2 branch Nov 30, 2022
@ramvikrams ramvikrams reopened this Nov 30, 2022
update the examples  where `re.match` and `re.search` shows a match but not `re.fullmatch`
Doc/library/re.rst Show resolved Hide resolved
<re.Match object; span=(0, 1), match='c'>
>>> re.search("c", "cdef") # Match
<re.Match object; span=(0, 1), match='c'>
>>> re.fullmatch("c", "cdef") # No Match
Copy link
Member

@ericvsmith ericvsmith Nov 30, 2022

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
Contributor Author

@ramvikrams ramvikrams Nov 30, 2022

Choose a reason for hiding this comment

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

yes sir

Copy link
Contributor Author

@ramvikrams ramvikrams Nov 30, 2022

Choose a reason for hiding this comment

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

done sir

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

@ericvsmith ericvsmith Nov 30, 2022

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
Contributor Author

@ramvikrams ramvikrams Nov 30, 2022

Choose a reason for hiding this comment

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

yes 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
Member

@ericvsmith ericvsmith Nov 30, 2022

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

@ramvikrams ramvikrams Nov 30, 2022

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

@ericvsmith ericvsmith Nov 30, 2022

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.

@bedevere-bot
Copy link

bedevere-bot commented Nov 30, 2022

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ramvikrams
Copy link
Contributor Author

ramvikrams commented Nov 30, 2022

I have made the requested changes; please review again

@bedevere-bot
Copy link

bedevere-bot commented Nov 30, 2022

Thanks for making the requested changes!

@ericvsmith: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from ericvsmith Nov 30, 2022

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

@ericvsmith ericvsmith Nov 30, 2022

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

@ericvsmith ericvsmith Nov 30, 2022

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.

+ :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
Member

@ericvsmith ericvsmith Nov 30, 2022

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.

@bedevere-bot
Copy link

bedevere-bot commented Nov 30, 2022

Thanks for making the requested changes!

@ericvsmith: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from ericvsmith Nov 30, 2022
Copy link
Member

@ericvsmith ericvsmith left a comment

Sorry for one additional small change. I'll commit it after this change.


For example::

>>> re.match("c", "abcdef") # No match
>>> re.search("c", "abcdef") # Match
<re.Match object; span=(2, 3), match='c'>
>>> re.fullmatch("p.*n", "python")
Copy link
Member

@ericvsmith ericvsmith Nov 30, 2022

Choose a reason for hiding this comment

The 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?

@bedevere-bot
Copy link

bedevere-bot commented Nov 30, 2022

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ericvsmith ericvsmith self-assigned this Nov 30, 2022
@ramvikrams
Copy link
Contributor Author

ramvikrams commented Nov 30, 2022

I have made the requested changes; please review again

@bedevere-bot
Copy link

bedevere-bot commented Nov 30, 2022

Thanks for making the requested changes!

@ericvsmith: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from ericvsmith Nov 30, 2022
@ericvsmith ericvsmith merged commit e0f91de into python:main Nov 30, 2022
14 checks passed
@miss-islington
Copy link
Contributor

miss-islington commented Nov 30, 2022

Thanks @ramvikrams for the PR, and @ericvsmith for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11.
🐍🍒🤖

@bedevere-bot
Copy link

bedevere-bot commented Nov 30, 2022

GH-99912 is a backport of this pull request to the 3.11 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 30, 2022
…ould mention ```fullmatch()``` (pythonGH-98916)

Mention fullmatch along with search and match.
(cherry picked from commit e0f91de)

Co-authored-by: ram vikram singh <ramvikrams243@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 30, 2022
…ould mention ```fullmatch()``` (pythonGH-98916)

Mention fullmatch along with search and match.
(cherry picked from commit e0f91de)

Co-authored-by: ram vikram singh <ramvikrams243@gmail.com>
@bedevere-bot
Copy link

bedevere-bot commented Nov 30, 2022

GH-99913 is a backport of this pull request to the 3.10 branch.

@ramvikrams
Copy link
Contributor Author

ramvikrams commented Nov 30, 2022

thanks sir for your help

ericvsmith pushed a commit that referenced this pull request Nov 30, 2022
…hould mention ```fullmatch()``` (GH-98916) (GH-99912)

GH-98906 ```re``` module: ```search() vs. match()``` section should mention ```fullmatch()``` (GH-98916)

Mention fullmatch along with search and match.
(cherry picked from commit e0f91de)

Co-authored-by: ram vikram singh <ramvikrams243@gmail.com>

Co-authored-by: ram vikram singh <ramvikrams243@gmail.com>
ericvsmith pushed a commit that referenced this pull request Nov 30, 2022
…hould mention ```fullmatch()``` (GH-98916) (GH-99913)

GH-98906 ```re``` module: ```search() vs. match()``` section should mention ```fullmatch()``` (GH-98916)

Mention fullmatch along with search and match.
(cherry picked from commit e0f91de)

Co-authored-by: ram vikram singh <ramvikrams243@gmail.com>

Co-authored-by: ram vikram singh <ramvikrams243@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants