Regular Expression pattern group

J

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".

Traceback (most recent call last):
File "<pyshell#279>", line 1, in -toplevel-
m.group(4)
IndexError: no such group
p.findall(s) [('???', '?', 'ef')]
 
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top