catastrophic regexp, help!

cirfu circularfunc at yahoo.se
Wed Jun 11 23:07:26 EDT 2008


On 11 Juni, 10:25, Chris <cwi... at gmail.com> wrote:
> On Jun 11, 6:20 am, cirfu <circularf... at yahoo.se> 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


yes, seems very unpythonic though :)
must be a simpler way that isnt slow as hell.



More information about the Python-list mailing list