Regular Expression pattern group

JH john.hsu at
Thu Jun 15 08:13:59 CEST 2006


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 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
>>> m =
>>> m.groups()
('???', '?', 'ef')

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

More information about the Python-list mailing list