[Tutor] don't understand iteration
Alan Gauld
alan.gauld at btinternet.com
Tue Nov 11 02:14:17 CET 2014
On 11/11/14 00:52, Clayton Kirkwood wrote:
>> But group() - singular - returns a single group item which is always a
>> string. You use group() to get the matching substring. You use groups to
>> find all the substrings.
>
> I believe that is true only if you are receiving a single return value. If
> it is more than one group, it returns a tuple (I guess of strings).
Yes, sorry. Thats what I meant. groups returns a tuple of substrings. So
len(groups) is the len that you are looking for - the number of groups
found.
But you should already know the answer since you are creating the groups
in your pattern and the search only returns a valid match object if all
the groups match. So the number of groups found is always the number of
groups defined.
So unless you are doing something really messy, like defining the regex
dynamically and adding groups on the fly you rarely need to find out how
many groups are returned, because you wrote the pattern in the first place!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list