Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why not match.expand('\\0')?

Reply
Thread Tools

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

 
 
Magnus Lie Hetland
Guest
Posts: n/a
 
      12-12-2003
I'm using the expand method of re MatchObjects, and was surprised to
find that the references don't work with group 0...

>>> import re
>>> p = re.compile('foo')
>>> m = p.match('foo')
>>> m.group(0)

'foo'
>>> m.expand('\\0')

'\x00'

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

--
Magnus Lie Hetland "The mind is not a vessel to be filled,
http://hetland.org but a fire to be lighted." [Plutarch]
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-13-2003
Magnus Lie Hetland wrote:

> I'm using the expand method of re MatchObjects, and was surprised to
> find that the references don't work with group 0...
>
> >>> import re
> >>> p = re.compile('foo')
> >>> m = p.match('foo')
> >>> m.group(0)

> 'foo'
> >>> m.expand('\\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>




 
Reply With Quote
 
 
 
 
Magnus Lie Hetland
Guest
Posts: n/a
 
      12-13-2003
In article <(E-Mail Removed)>,
Fredrik Lundh wrote:
>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...

></F>


--
Magnus Lie Hetland "The mind is not a vessel to be filled,
http://hetland.org but a fire to be lighted." [Plutarch]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why :: ? Why not : ? Why not . ? <- less clutter ?!? Skybuck Flying C++ 16 08-25-2007 09:48 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM



Advertisments