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


Groups > comp.lang.python > #56012 > unrolled thread

returning all matching groups with re.search()

Started by"mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" <mhearne808@gmail.com>
First post2011-02-03 12:32 -0800
Last post2011-02-03 12:42 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#56012 — returning all matching groups with re.search()

From"mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" <mhearne808@gmail.com>
Date2011-02-03 12:32 -0800
Subjectreturning all matching groups with re.search()
Message-ID<ed19857d-3b5e-4c46-87b3-9dedb0ad20ed@a28g2000vbo.googlegroups.com>
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?

[toc] | [next] | [standalone]


#56036

FromChris Rebert <clp2@rebertia.com>
Date2011-02-03 12:42 -0800
Message-ID<mailman.1630.1296765779.6505.python-list@python.org>
In reply to#56012
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web