Re: catastrophic regexp, help!
- From: Chris <cwitts@xxxxxxxxx>
- Date: Wed, 11 Jun 2008 01:25:58 -0700 (PDT)
On Jun 11, 6:20 am, cirfu <circularf...@xxxxxxxx> wrote:
pat = re.compile("(\w* *)*")
this matches all sentences.
if fed the string "are you crazy? i am" it will return "are you
crazy".
i want to find a in a big string a sentence containing Zlatan
Ibrahimovic and some other text.
ie return the first sentence containing the name Zlatan Ibrahimovic.
patzln = re.compile("(\w* *)* zlatan ibrahimovic (\w* *)*")
should do this according to regexcoach but it seems to send my
computer into 100%CPU-power and not closable.
Maybe something like this would be of use...
def sentence_locator(s, sub):
cnt = s.upper().count(sub.upper())
if not cnt:
return None
tmp = []
idx = -1
while cnt:
idx = s.upper().find(sub.upper(), (idx+1))
a = -1
while True:
b = s.find('.', (a+1), idx)
if b == -1:
b = s.find('.', idx)
if b == -1:
tmp.append(s[a+1:])
break
tmp.append(s[a+1:b+1])
break
a = b
cnt -= 1
return tmp
.
- Follow-Ups:
- Re: catastrophic regexp, help!
- From: cirfu
- Re: catastrophic regexp, help!
- References:
- catastrophic regexp, help!
- From: cirfu
- catastrophic regexp, help!
- Prev by Date: Re: Why does python not have a mechanism for data hiding?
- Next by Date: Re: mysql to sqlite
- Previous by thread: Re: catastrophic regexp, help!
- Next by thread: Re: catastrophic regexp, help!
- Index(es):
Relevant Pages
|