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

Regular Expression pattern group

P: n/a
JH
Hi

I am a fussy learner. Could someone explain to me why the following
inconsistency exists between methods? How can it be justified if it is
considered all right?

There are three groups in pattern. However, match object shows 3 groups
in collection, but group has to be indexed from one because the
m.group(0) is the implicit group for whole pattern.

Are these just some things to remember? "Group counting is from one".
p = sre.compile('abc(.(.).)d(ef)')
s 'xxxabc???defxxx' m = p.search(s)
m.groups() ('???', '?', 'ef') m.group(0) 'abc???def' m.group(1) '???' m.group(2) '?' m.group(3) 'ef' m.group(4)
Traceback (most recent call last):
File "<pyshell#279>", line 1, in -toplevel-
m.group(4)
IndexError: no such group p.findall(s) [('???', '?', 'ef')]


Jun 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ant
I am a fussy learner. Could someone explain to me why the following
inconsistency exists between methods? How can it be justified if it is
considered all right?


It's the standard way of accessing groups from regex matches in pretty
much all languages that support them. In most modern languages, I
believe regexes are generally designed to be compatible with Perl
regexes - but these I guess derive from earlier languages like awk and
sed.

So it's not an inconsistency. Think of 0 as being an implicit group
around the entire expression.

In addition, if the match around the entire group was only accessible
via a different method, then how would you refer to the entire match in
back-references/substitutions?

Jun 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.