[Tutor] don't understand iteration
Alan Gauld
alan.gauld at btinternet.com
Tue Nov 11 00:58:35 CET 2014
On 10/11/14 23:08, Clayton Kirkwood wrote:
>>> I couldn't find a way to get the len of blah.
>>
>> What would you expect the answer to be?
>
> I would expect len(sizeof, whatever)(blah) to return the number of (in this
> case) matches, so 5.
But remember that search is matching the pattern, not the groups -
that's a side effect. So len(matchObject) could just as validly
return the length of the string that matched the pattern.
It just depends on the content that you are using.
> of matches. Why else would you do a search
To look for a pattern. I very rarely use groups in regex - and
I try not to use regex at all! If I can find the matching substring
I will likely be able to pull out the details using regular string
methods or other tools. For example in your case I might have tried
using time.strparse()
> certainly wouldn't want len(group) to return the number of characters, in
> this case, 28 (which it does:>{{{
But group() - with default group of 0 - returns the whole matched
substring and len() on a string returns the number of characters.
You want len(match.groups()) to get the number of matches.
> I didn't run group to find out the number of characters in a string, I ran
> it to find out something about blah and its matches.
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.
--
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