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

Discussion in 'Python' started by Magnus Lie Hetland, Dec 12, 2003.

  1. 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]
     
    Magnus Lie Hetland, Dec 12, 2003
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, Dec 13, 2003
    #2
    1. Advertising

  3. In article <>,
    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]
     
    Magnus Lie Hetland, Dec 13, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    653
  2. Victor
    Replies:
    2
    Views:
    667
    Victor
    May 17, 2004
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    977
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,217
    Smokey Grindel
    Dec 2, 2006
  5. Andrew Bailey
    Replies:
    9
    Views:
    671
    richard
    Apr 16, 2008
Loading...

Share This Page