returning all matching groups with re.search()
clp2 at rebertia.com
Thu Feb 3 21:42:55 CET 2011
On Thu, Feb 3, 2011 at 12:32 PM,
<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').
Straight from the docs (http://docs.python.org/library/re.html ), emphasis mine:
re.search(pattern, string[, flags])
"Scan through string looking for **a** location where the regular
expression pattern produces **a** match [...]"
Hence, it stops looking after the very first match.
> 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?
I think you want re.finditer() or re.findall().
More information about the Python-list