Regular Expression Grouping

Steve Holden steve at holdenweb.com
Sun Aug 12 13:54:13 EDT 2007


linnewbie at gmail.com 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()
('abc',)
 >>>

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?

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- 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