Some more odd behaviour from the Regexp library
mwm at mired.org
Thu Oct 20 05:48:25 CEST 2005
"David Veerasingam" <vdavidster at gmail.com> writes:
> Can anyone explain why it won't give me my captured group?
> In : a = 'exit: gkdfjgfjdfsgdjglkghdfgkd'
> In : import re
> In : b = re.search(r'exit: (.*?)', a)
> In : b.group(0)
> Out: 'exit: '
> In : b.group(1)
> Out: ''
> In : b.group(2)
> IndexError: no such group
It is giving you your captured group. While the * operator matches as
long a string as possible, the *? operator matches as *short* a string
as possible. Since '' matches .*?, that's all it's ever going to
capture. So b.group(1) is '', which is what it's giving you.
>>> a = 'exit: gkdfjgfjdfsgdjglkghdfgkd'
>>> import re
>>> b = re.search(r'exit: (.*)', a)
which I suspect is what you actually want.
Of course, being the founder of SPARE, I have to point out that
a.split(': ') will get you the same two strings as the re I used
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list