[Python-Dev] a feature i'd like to see in python #2: indexing of match objects
Steve Holden
steve at holdenweb.com
Sun Dec 3 21:41:04 CET 2006
Martin v. Löwis wrote:
> Fredrik Lundh schrieb:
>>>> match groups are numbered 1..N, not 0..(N-1), in both the API and in the
>>>> RE syntax (and we don't have much control over the latter).
>>> py> m = re.match("a(b)","ab")
>>> py> m.group(0)
>>> 'ab'
>>> py> m.group(1)
>>> 'b'
>> 0 isn't a group, it's an alias for the full match.
>
> So what is the proper term for the things that the .group() method
> returns? According to
>
> http://docs.python.org/lib/match-objects.html
>
> it returns "subgroups of the match".
>
> So the things to be indexed in this proposal are subgroups of the
> match.
>
Precisely. But your example had only one group "(b)" in it, which is
retrieved using m.group(1). So the subgroups are numbered starting from
1 and subgroup 0 is a special case which returns the whole match.
I know what the Zen says about special cases, but in this case the rules
were apparently broken with impunity.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
More information about the Python-Dev
mailing list