Regular Expression Grouping

linnewbie at gmail.com linnewbie at gmail.com
Sun Aug 12 13:50:06 EDT 2007


On Aug 12, 1:31 pm, Fabio Z Tessitore <fabioztessit... at libero.it>
wrote:
> Il Sun, 12 Aug 2007 17:21:02 +0000, linnewbie ha scritto:
>
> > Fairly new to this regex thing, so this might be very juvenile but
> > important.
>
> > I cannot understand and why 'c' constitutes a group here without being
> > surrounded by "(" ,")" ?
>
> >>>>import re
> >>>> m = re.match("([abc])+", "abc")
> >>>> m.groups()
> > ('c',)
>
> > Grateful for any clarity.
>
> thera are () outer [], maybe you don't know what do [] mean? or you want
> to know why 'c' and not 'a' or 'b'
> bye

I sort of get what the metacharacters  "(", ")"  and "[" ,"]" , groups
are marked by the "(", ")" no?

So I get this:

>>> import re
>>> re.match("ab(c)d","abcd")
<_sre.SRE_Match object at 0xb7d72c60>
>>> m= re.match("ab(c)d","abcd")
>>> m.groups()
('c',)

I can see clearly here that 'c' is group(1), because of the "..(c)..
".  I cannot see how 'c' is a inner group in the expressions "([abc])
+" above?









More information about the Python-list mailing list