Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > match groups: optional groups not accessible

Reply
Thread Tools

match groups: optional groups not accessible

 
 
david.reitter@gmail.com
Guest
Posts: n/a
 
      06-10-2005
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.

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-10-2005
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
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      06-10-2005
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
>


 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-10-2005
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
 
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
Q: Adding Optional option to expression match Martin Daser Perl Misc 2 05-18-2006 08:35 AM
Groups of groups with PIX. AM Cisco 2 01-31-2006 04:37 PM
Windows groups, VPN groups, and SecureACS John Sasso Cisco 0 10-02-2004 03:39 PM
REGEX: capturing on optional groups which fail Charles Shannon Hendrix Perl 0 06-13-2004 11:22 PM
Reading membership of groups (including nested groups) Petra Hübner ASP .Net 0 02-16-2004 07:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57