Non-greedy regex?

Ken Ray kray at sonsothunder.com
Mon Aug 18 12:24:00 EDT 2003


>    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?

Yes. There are two ways. If you just want a certain match to be
"ungreedy", you can follow it with a ?, so something like:

  the (.*?) named

would find the first matched word between "the" and "named". So in the
sentence:

  the man named "Jack" spoke to the woman named "Jill"

it would find "man", not "woman" (whereas 'the (.*) named' would be
greedy and find "woman").

The second way is if you NEVER want it to be greedy in ANY match. In
this case, put "(?U)" before your pattern. So you'd have:

  put "(?U)(\n([^_]*)" & gClassMangle & "([^\n]*)\n)" into tPattern

Hope this makes sense,

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





More information about the use-livecode mailing list