>-----Original Message----- >From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On >Behalf Of Alan Gauld >Sent: Monday, November 10, 2014 3:59 PM >To: tutor@python.org >Subject: Re: [Tutor] don't understand iteration > >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.
Good point, although a nice side effect would be an added reference like search.matches which when accessed would return the value. It knows the value, it just isn't being made available. > >> 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() I'll explore > > >> 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. 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). Clayton > > >-- >Alan G >Author of the Learn to Program web site >http://www.alan-g.me.uk/ >http://www.flickr.com/photos/alangauldphotos > >_______________________________________________ >Tutor maillist - Tutor@python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor