match groups: optional groups not accessible

Discussion in 'Python' started by david.reitter@gmail.com, Jun 10, 2005.

  1. Guest

    Why does the following result in an IndexError?
    I try to match an optional group, and then access it via its group
    name. The group happens to not participate in the match, but is
    obviously defined in the pattern.
    The documentation says that, at least for numbered groups, "If a group
    is contained in a part of the pattern that did not match, the
    corresponding result is None.".
    So I would expect None rather than an IndexError, which is (only?)
    supposed to occur "If a string argument is not used as a group name in
    the pattern".

    I would expect named groups and numbered groups to be behave the same
    way.
    Where's my mistake?

    >>> import re
    >>> m = re.match('(?P<m>maybe)?yes', "yes")
    >>> m.group(1)
    >>> m.group('maybe')

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    IndexError: no such group

    PS.: Please cc your reply to my e-mail address.
     
    , Jun 10, 2005
    #1
    1. Advertising

  2. Peter Hansen Guest

    wrote:
    > Why does the following result in an IndexError?
    > I try to match an optional group, and then access it via its group
    > name. The group happens to not participate in the match, but is
    > obviously defined in the pattern.
    >
    >>>>m = re.match('(?P<m>maybe)?yes', "yes")


    Uh, don't you need to actually *group* the first "yes" part, with
    parentheses, to make it a group? Otherwise you aren't going to be
    treating it as other than text-between-groups-that-can-be-skipped.

    -Peter
     
    Peter Hansen, Jun 10, 2005
    #2
    1. Advertising

  3. Duncan Booth Guest

    david.reitter wrote:

    > So I would expect None rather than an IndexError, which is (only?)
    > supposed to occur "If a string argument is not used as a group name in
    > the pattern".

    That is exactly what does happen.

    >
    > I would expect named groups and numbered groups to be behave the same
    > way.
    > Where's my mistake?

    Using 'maybe' as a group name when the only group in the pattern is called
    'm'.

    >
    >>>> import re
    >>>> m = re.match('(?P<m>maybe)?yes', "yes")
    >>>> m.group(1)
    >>>> m.group('maybe')

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > IndexError: no such group
    >
     
    Duncan Booth, Jun 10, 2005
    #3
  4. Peter Hansen Guest

    Peter Hansen wrote:
    > wrote:
    >
    >> Why does the following result in an IndexError?
    >> I try to match an optional group, and then access it via its group
    >> name. The group happens to not participate in the match, but is
    >> obviously defined in the pattern.
    >>
    >>>>> m = re.match('(?P<m>maybe)?yes', "yes")

    >
    > Uh, don't you need to actually *group* the first "yes" part, with
    > parentheses, to make it a group? Otherwise you aren't going to be
    > treating it as other than text-between-groups-that-can-be-skipped.


    Oops... from Duncan's reply and a reread I can see I missed the point
    entirely... I obviously thought you were expecting the "yes" to be in a
    group too. Duh.

    -Peter
     
    Peter Hansen, Jun 10, 2005
    #4
    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. Charles Shannon Hendrix

    REGEX: capturing on optional groups which fail

    Charles Shannon Hendrix, Jun 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    571
    Charles Shannon Hendrix
    Jun 14, 2004
  2. Dave
    Replies:
    1
    Views:
    9,851
    Steven Cheng[MSFT]
    Oct 24, 2005
  3. hiwa
    Replies:
    0
    Views:
    653
  4. Guest
    Replies:
    1
    Views:
    562
    Andrea D'Onofrio [MSFT]
    May 21, 2004
  5. Martin Daser

    Q: Adding Optional option to expression match

    Martin Daser, May 18, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    102
    Martin Daser
    May 18, 2006
Loading...

Share This Page