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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Magnus Lie Hetland

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

    I'm using the expand method of re MatchObjects, and was surprised to
    find that the references don't work with group 0...
    [color=blue][color=green][color=darkred]
    >>> import re
    >>> p = re.compile('foo ')
    >>> m = p.match('foo')
    >>> m.group(0)[/color][/color][/color]
    'foo'[color=blue][color=green][color=darkred]
    >>> m.expand('\\0')[/color][/color][/color]
    '\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]
  • Fredrik Lundh

    #2
    Re: Why not match.expand('\ \0')?

    Magnus Lie Hetland wrote:
    [color=blue]
    > I'm using the expand method of re MatchObjects, and was surprised to
    > find that the references don't work with group 0...
    >[color=green][color=darkred]
    > >>> import re
    > >>> p = re.compile('foo ')
    > >>> m = p.match('foo')
    > >>> m.group(0)[/color][/color]
    > 'foo'[color=green][color=darkred]
    > >>> m.expand('\\0')[/color][/color]
    > '\x00'
    >
    > Is this intended behavior? Why should group 0 behave differently from
    > the others?[/color]

    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>




    Comment

    • Magnus Lie Hetland

      #3
      Re: Why not match.expand('\ \0')?

      In article <mailman.120.10 71315843.9307.p ython-list@python.org >,
      Fredrik Lundh wrote:[color=blue]
      >Magnus Lie Hetland wrote:
      >[/color]
      [snip][color=blue]
      >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)[/color]

      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...
      [color=blue]
      ></F>[/color]

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

      Comment

      Working...