[Tutor] Regular expressions: findall vs search

Steven D'Aprano steve at pearwood.info
Wed Jul 11 03:47:11 CEST 2012


Alexander Q. wrote:

> My question is how does Python know to return just the part in the
> parentheses and not to return the "blahblah" and the "yattayattayatta",
> etc...? The 're.search' function returns the whole thing, and if I want
> just the parentheses parts, I do tuples.group(1) or tuples.group(2) or
> tuples.group(3), depending on which set of parentheses I want.

The Fine Manual explains this. Is it not clear enough? If not, what parts are 
unclear -- we always appreciate suggested improvements.

http://docs.python.org/library/re.html#re.findall

Or at the interactive prompt, type:

help(re.findall)

which gives you:

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.




-- 
Steven


More information about the Tutor mailing list