Regular expression

Ken Ray kray at sonsothunder.com
Sat Mar 13 02:01:55 EST 2010


> on mouseUp
>    local mySource,myStart,myEnd,TESTO,myStart2,myEnd2
>    put "xxxxxx src='HTTPWEB' width='3845' height='334' src='HTTPWEB' xxxxx"
> into TESTO
>    put empty into mySource
>    put empty into myStart
>    put empty into myEnd
>    put matchChunk(TESTO,"src='([^']*)'",myStart,myEnd,myStart2,myEnd2)
>    put myStart & comma & myEnd & comma &  myStart2 & comma & myEnd2
> end mouseUp
> 
> This script returns "13,19,," .
>  I exprected something like "13,19,57,63" , because now I have two strings
> matching "src='([^']*)'" .

Sorry, Paolo, that's not how matchChunk works. You only get a single match
on a given "capture" region (what's inside the parentheses). Normally this
match is "greedy" - that is, it will give you the *largest* match it can in
the string you're evaluating. You can modify that by providing the "?" and
turn it "non-greedy", which will give you the *shortest* match.

So with a string of:

    The hot dog in the hotel was hotter than usual.

get matchText(tString,"(h.*t)",tMatch)
put tMatch
--> he hot dog in the hotel was hotter t

get matchText(tString,"(h.*?t)",tMatch)
put tMatch
--> he hot

get matchChunk(tString,"(hot)",tStart,tEnd)
put tStart,tEnd
--> 5,7

Of course, you can provide multiple capture regions if you know how many
regions there will be in the string, but note that the "greediness" applies
here too, so watch when you use ? and when you don't:

get matchChunk(tString,"(hot).*(hot)",tStart,tEnd,tStart2,tEnd2)
put tStart,tEnd,tStart2,tEnd2
--> 5,7,30,32  -- matched first "hot", and "hot" in "hotter"

get matchChunk(tString,"(hot).*?(hot)",tStart,tEnd,tStart2,tEnd2)
put tStart,tEnd,tStart2,tEnd2
--> 5,7,20,22  -- matched first "hot", and "hot" in "hotel"

If you don't know the number of possible capture regions, you'll have to use
a repeat loop and limit the string by deleting up to what has matched the
last time for each iteration:

on mouseUp
  put "The hot dog in the hotel was hotter than usual." into tString
  put "" into tMatches
  put 0 into tAdder
  repeat
    if matchChunk(tString,"(hot)",tStart,tEnd) then
      put (tStart + tAdder) & "," & (tEnd + tAdder) & cr after tMatches
      add tEnd to tAdder
      delete char 1 to tEnd of tString
    else
      exit repeat
    end if
  end repeat
  put tMatches
end mouseUp

--> 5,7
      20,22
      30,32

Anyway, you get the idea...


Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/





More information about the use-livecode mailing list