Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56036

Re: returning all matching groups with re.search()

References <ed19857d-3b5e-4c46-87b3-9dedb0ad20ed@a28g2000vbo.googlegroups.com>
Date 2011-02-03 12:42 -0800
Subject Re: returning all matching groups with re.search()
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.1630.1296765779.6505.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Feb 3, 2011 at 12:32 PM,
mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
<mhearne808@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().

Cheers,
Chris
--
http://blog.rebertia.com

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

returning all matching groups with re.search() "mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" <mhearne808@gmail.com> - 2011-02-03 12:32 -0800
  Re: returning all matching groups with re.search() Chris Rebert <clp2@rebertia.com> - 2011-02-03 12:42 -0800

csiph-web