Non-greedy regex?
Jim Witte
jswitte at bloomington.in.us
Mon Aug 18 10:39:01 EDT 2003
Hi,
I want to scan a long chunk of multiline text and extract all lines
that contain the string gClassMangle (in the form of '__##<classname>'
where ## is the length of the class name). I'm was trying to use the
following script - the basic idea is to find the first match, get it,
then delete everything up to and including it from the text and repeat.
put "__" & tClassLength & gClassName into gClassMangle
put "(\n([^_]*)" & gClassMangle & "([^\n]*)\n)" into tPatter
repeat forever
put empty into startMatch
put empty into endMatch
if matchChunk(gJumpText, tPattern, startMatach, endMatch) then
get char startMatch to endMatch of gJumpText
answer it
delete char 1 to endMatch of gJumpText
else
exit repeat
end if
end repeat
But it was matching everything from the beginning of the text to the
end of the first match. I realized this was because it was doing a
greedy match (as regex is), and so didn't just match from the nearest
return character (beginning of the line), but from the first return
character (beginning of the text).
Is there an elegant way (without having to count back characters and
such from endMatch) to get matchChunk to do what I want and do a
"non-greedy" match?
Jim
More information about the use-livecode
mailing list