By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

match groups: optional groups not accessible

P: n/a
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.

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
da***********@gmail.com 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
Jul 19 '05 #2

P: n/a
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


Jul 19 '05 #3

P: n/a
Peter Hansen wrote:
da***********@gmail.com 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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.