This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: re fullmatch error with non greedy modifier
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.4, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: danielhrisca, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-12-14 07:56 by danielhrisca, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg308278 - (view) Author: Daniel Hrisca (danielhrisca) Date: 2017-12-14 07:56
Consider this code snippet:


from re import match, fullmatch

pattern = '".+?"'
string = '"hello" "again"'

print(match(pattern, string))
print(fullmatch(pattern, string))


Which prints:
<_sre.SRE_Match object; span=(0, 7), match='"hello"'>
<_sre.SRE_Match object; span=(0, 15), match='"hello" "again"'>

The fullmatch function seems to ignore the non-greedy modifier.

From the fullmatch docstring I expected that fullmatch is equivalent to:

def fullmatch(pattern, string):
    match = re.match(pattern, string)
    if match:
        if match.start() == 0 and match.end() == len(string):
            return match
        else:
            return None
    else:
        return None
msg308279 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-14 08:10
No, this is not how fullmatch() works. Otherwise there wouldn't be need to add it into the stdlib. You could just check end() of the result.

fullmatch() acts like match() with added \Z. But if the pattern contains multiple alternates you need to add \Z at the end of every branch (r"foo\Z|bar\Z") or wrap the entire patch in a non-capturing group (r"(?:foo|bar)\Z").
History
Date User Action Args
2022-04-11 14:58:55adminsetgithub: 76500
2017-12-14 08:10:33serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg308279

resolution: not a bug
stage: resolved
2017-12-14 07:56:47danielhriscacreate