Re: catastrophic regexp, help!



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
.



Relevant Pages

  • Re: Calculating a time and date (counting down)
    ... Function FormatUnits(ByVal Period As Date, Pattern As String, Optional Start As Date) As String ... Dim idx As Long, pat As Long, cnt As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: catastrophic regexp, help!
    ... if fed the string "are you crazy? ... if not cnt: ... idx = -1 ... expressions and groups will cause python's regular expression engine ...
    (comp.lang.python)
  • Re: catastrophic regexp, help!
    ... if fed the string "are you crazy? ... if not cnt: ... idx = -1 ... return tmp ...
    (comp.lang.python)
  • Re: Text importer
    ... Public Sub Add(ByRef Field As String, ByVal Position As Long, ByVal Length As Long) ... Public Property Let Text ... Dim pos As Long, siz As Long, idx As Long ... Dim tmp As String ...
    (microsoft.public.vb.general.discussion)
  • RE: VB & Common Dialog???
    ... lpstrCustomFilter As String ... Private Declare Function SHGetPathFromIDList Lib "shell32" _ ... Dim sBuffer As String ... Dim Idx As Long ...
    (microsoft.public.vb.general.discussion)