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: '?' is always non-greedy
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Sworddragon, ezio.melotti, pitrou, serhiy.storchaka, tim.peters
Priority: normal Keywords:

Created on 2013-12-12 17:07 by Sworddragon, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py Sworddragon, 2013-12-12 17:07
Messages (3)
msg205962 - (view) Author: (Sworddragon) Date: 2013-12-12 17:07
From the documentation: "The '*', '+', and '?' qualifiers are all greedy;"

But this is not the case for '?'. In the attachments is an example which shows this: re.search(r'1?', '01') should find '1' but it doesn't find anything.
msg205965 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-12-12 17:29
I don't think the documentation is wrong. re.search() returns first match, and this is empty string at position 0.

>>> import re
>>> re.search('1?', '01')
<_sre.SRE_Match object; span=(0, 0), match=''>

All matches:

>>> list(re.findall('1?', '01'))
['', '1', '']
>>> list(re.finditer('1?', '01'))
[<_sre.SRE_Match object; span=(0, 0), match=''>, <_sre.SRE_Match object; span=(1, 2), match='1'>, <_sre.SRE_Match object; span=(2, 2), match=''>]
msg205966 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2013-12-12 17:32
It's working fine.  `.search()` always finds the leftmost position at which the pattern matches.  In your example, the pattern '1?' does match at index 0:  it first tries to match `1' at index 0.  That's the greedy part.  The attempt fails, so it next tries to match the empty string at index 0.  That succeeds, which you can see by printing search.span(0) (which displays (0, 0)).

Of course you'd get exactly the same result if you tried matching `1*` instead.  But you'd get a different result from matching '1+', because that pattern does _not_ match at index 0.  In that case the engine has to move to index 1 to get a match.

And if you search the string '11' with the pattern '1?`, you'll see that it does match the slice 0:1.  If, as you claimed, ? were not greedy, it would match the empty string 0:0 instead.
History
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64163
2013-12-12 17:32:32tim.peterssetstatus: open -> closed
2013-12-12 17:32:25tim.peterssetstage: resolved
2013-12-12 17:32:10tim.peterssetnosy: + tim.peters
messages: + msg205966
2013-12-12 17:29:31serhiy.storchakasetnosy: + ezio.melotti, serhiy.storchaka, pitrou
resolution: not a bug
messages: + msg205965
2013-12-12 17:07:47Sworddragoncreate