returning all matching groups with re.search()

  • Thread starter mhearne808[insert-at-sign-here]gmail[insert-dot-he
  • Start date
M

mhearne808[insert-at-sign-here]gmail[insert-dot-he

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?
 
C

Chris Rebert

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
 
B

Benjamin Kaplan

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:('e',)

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

Roland Mueller

Hello,

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.
['e', 'e', 'e']

depending on what is intended the non-overlapping behaviour of findall()
may cause some problem:
>>> re.findall('.[aeiou].','fredbarneybettywilma')
['red', 'bar', 'ney', 'bet', 'wil']
>>> re.findall('.[aeiouy].','fredbarneybettywilma')
['red', 'bar', 'ney', 'bet', 'tyw']

BR,
Roland
 
M

MRAB

Hello,

import re
help(re.findall)
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.
re.findall('e','fredbarneybettywilma')
['e', 'e', 'e']

depending on what is intended the non-overlapping behaviour of findall()
may cause some problem:
re.findall('.[aeiou].','fredbarneybettywilma')
['red', 'bar', 'ney', 'bet', 'wil']
re.findall('.[aeiouy].','fredbarneybettywilma')
['red', 'bar', 'ney', 'bet', 'tyw']
Overlapping matches are supported by this regex module:

http://pypi.python.org/pypi/regex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top