returning all matching groups with re.search()

Benjamin Kaplan benjamin.kaplan at case.edu
Thu Feb 3 21:45:55 CET 2011


On Thu, Feb 3, 2011 at 3:32 PM,
mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
<mhearne808 at gmail.com> wrote:
> Here's a scenario:
>
> import re
> m = re.search('e','fredbarneybettywilma')
>
> Now, here's a stupid question:
> why doesn't m.groups() return ('e','e','e').
>
> I'm trying to figure out how to match ALL of the instances of a
> pattern in one call - the group() and groups() return subgroups... how
> do I get my search to get me all of the matching subgroups?

m.groups() doesn't give return ('e','e','e') because groups don't mean
what you think they mean. Groups are subsections of a regular
expression, enclosed by parenthesis. For example:
>>> m = re.search('(e)','fredbarneybettywilma')
>>> m.groups()
('e',)

What you want seem to want is re.findall, not re.search



More information about the Python-list mailing list