Regular Expression Groups - loop

Peter Otten __peter__ at web.de
Tue Aug 7 04:03:32 EDT 2007


 shahargs at gmail.com wrote:

> I'm trying to write application which parse one page to sentences and
> then, check every group for few things.
> 
> The code:
> rawstr = r"""([^.?!]+[.?!])"""
> regex=re.compile(rawstr)
> matchs=regex.search(document)
> document, is the text i parsing. I cheked it in Kodos, and everything
> worked well. but, i'm trying to write for loop which check every
> group.
> 
> I tried:
> for group in matchs.groups():
> but then, it's check only the first group. and i tried:
> for group in matchs.group():
> but then it's checking every letter, not every sentence.
> 
> is anyone know how i should write this loop to check every group on
> the groups collection that the regular expression return?

A group denotes a subexpression of a single match:

>>> re.compile("(a+)(b*)").search("xaaabbxbb").groups()
('aaa', 'bb')

To get multiple matches use the findall() or finditer() methods, e. g.

>>> for match in  re.compile("([^.?!]+[.?!])").finditer("Is that what you
want? Yes! At least I think so..."):
...     match.group()
...
'Is that what you want?'
' Yes!'
' At least I think so.'

Peter



More information about the Python-list mailing list