returning all matching groups with re.search()

Roland Mueller roland.em0001 at googlemail.com
Fri Feb 11 05:34:13 CET 2011


Hello,

On 02/07/2011 06:26 PM, Mauro Caceres wrote:
> >>> import re
> >>> help(re.findall)
> Help on function findall in module re:
>
> findall(pattern, string, flags=0)
>     Return a list of all non-overlapping matches in the string.
>     If one or more groups are present in the pattern, return a
>     list of groups; this will be a list of tuples if the pattern
>     has more than one group.
>     Empty matches are included in the result.
>
> >>> re.findall('e','fredbarneybettywilma')
> ['e', 'e', 'e']

depending on what is intended the non-overlapping behaviour of findall() 
may cause some problem:

 >>> re.findall('.[aeiou].','fredbarneybettywilma')
['red', 'bar', 'ney', 'bet', 'wil']

 >>> re.findall('.[aeiouy].','fredbarneybettywilma')
['red', 'bar', 'ney', 'bet', 'tyw']

BR,
Roland
>
> -- 
> Mauro Cáceres




More information about the Python-list mailing list