Regular Expression Groups - loop

S

shahargs

Hi,
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?

Shahar.
 
P

Peter Otten

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:
('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
 
M

Marc 'BlackJack' Rintsch

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:

There is only one group in your regular expression. I guess you are
confusing groups within one match with multiples matches in the text.
`re.search()` finds exactly one sentence. If you want all sentences use
`re.find_all()` or `re.find_iter()`.

Ciao,
Marc 'BlackJack' Rintsch
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top