Regular Expression Grouping

Steve Holden steve at
Sun Aug 12 19:54:13 CEST 2007

linnewbie at wrote:
> 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.
What's happening there is that the same group is being used three times 
to complete the match, but a group can only be represented once in the 
output, so you are seeing the last substring that the group matched. 
Contrast with:

 >>> m = re.match("([abc]+)", 'abc')
 >>> m.groups()

I don't *think* there's any way to introduce a variable number of groups 
into your match, but I don't use re's that much so someone may be able 
to help if that's what you want. Is it?

Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd 
Skype: holdenweb
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

More information about the Python-list mailing list