Why not match.expand('\\0')?

M

Magnus Lie Hetland

I'm using the expand method of re MatchObjects, and was surprised to
find that the references don't work with group 0...
'\x00'

Is this intended behavior? Why should group 0 behave differently from
the others?
 
F

Fredrik Lundh

Magnus said:
I'm using the expand method of re MatchObjects, and was surprised to
find that the references don't work with group 0...

'\x00'

Is this intended behavior? Why should group 0 behave differently from
the others?

because there is no group 0; the m.group(0) call is just a shortcut
to get the entire match.

(try m.groups() if you don't believe me)

</F>
 
M

Magnus Lie Hetland

Magnus Lie Hetland wrote:
[snip]
because there is no group 0; the m.group(0) call is just a shortcut
to get the entire match.

(try m.groups() if you don't believe me)

Hehe. I believe you -- and I know how this behaves. I guess I'm
questioning the behavior. You say "there is no group 0", while I say
"why isn't group 0 in m.groups(), m.groupdict() and so on?"

In other words, I see "group 0" as a name for the entire match, just
as m.group(0) is a shortcut to it. It clearly exists, in some sense,
but whether it is actually a group or not -- well, I'm quite sure you
are right in the matter. (IIRC, other languages such as AWK don't
treat this as a "short-cut", but as a real group in its own right...
But I may not RC :)

I guess I just think it would be useful to extend the "shortcut" of
the "group" 0 to more than just the group method. There are (IMO)
cases where referencing the entire match (e.g. as '\\0') can be
useful. Of course one can add an extra set of parentheses, but in my
case that's not really feasible.

As it is, I have simply written a wrapper function to deal with the
special case. But I was pretty confused about my unit test output for
a while there...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top