Title: re: match of nongreedy regex not grouping right
Type: Stage: resolved
Components: Library (Lib) Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jaredgrubb, r.david.murray
Priority: normal Keywords:

Created on 2013-01-17 23:49 by jaredgrubb, last changed 2013-01-18 00:21 by jaredgrubb. This issue is now closed.

Messages (5)
msg180162 - (view) Author: Jared Grubb (jaredgrubb) Date: 2013-01-17 23:49
re.match matches, but the capture groups are empty. That's not possible.

Python 2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.match('.*', 'stuff').group()  # greedy matches and captures; cool.
>>> re.match('.*?', 'stuff').group() # nongreedy matches (cool) but doesnt capture (huh?)
msg180164 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-18 00:12
Wouldn't a non-greedy .* match the null string?
msg180165 - (view) Author: Jared Grubb (jaredgrubb) Date: 2013-01-18 00:14
>>> re.match('.*', '')
<_sre.SRE_Match object at 0x107c6d308>
>>> re.match('.*?', '')
<_sre.SRE_Match object at 0x107c6d370>
msg180166 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-18 00:16
So, group() is returning the correct value, then.
msg180167 - (view) Author: Jared Grubb (jaredgrubb) Date: 2013-01-18 00:21
You're right. My mistake. I thought "match" meant "the full string must match", but in Python it means "the beginning must match".

Sorry for the noise.
Date User Action Args
2013-01-18 00:21:39jaredgrubbsetmessages: + msg180167
2013-01-18 00:16:53r.david.murraysetstatus: open -> closed
resolution: not a bug
messages: + msg180166

stage: resolved
2013-01-18 00:14:07jaredgrubbsetmessages: + msg180165
2013-01-18 00:12:21r.david.murraysetnosy: + r.david.murray
messages: + msg180164
2013-01-17 23:49:03jaredgrubbcreate