Regular Expression Grouping

Duncan Booth duncan.booth at invalid.invalid
Sun Aug 12 13:44:36 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.
> 
The group matches a single letter a, b, or c. That group must match one or 
more times for the entire expression to match: in this case it matches 3 
times once for the a, once for the b and once for the c. When a group 
matches more than once, only the last match is available, i.e. the 'c'. The 
matches against the a and b are discarded.

Its a bit like having some code:

   x = 'a'
   x = 'b'
   x = 'c'
   print x

and asking why x isn't 'a' and 'b' as well as 'c'.



More information about the Python-list mailing list