Standard Library

Kay C Lan lan.kc.macmail at gmail.com
Sat Aug 9 23:15:49 EDT 2014


On Sun, Aug 10, 2014 at 9:08 AM, JB <sundown at pacifier.com> wrote:
> One of the things I am interested in seeing more handlers
> and examples is regex.  It seems to be a very powerful
> tool with very few LiveCode examples.

The matchChunk and matchText functions are pretty straight forward to
use, it's building the regex that can be difficult if you don't live
with it regularly. If what you really need is examples of regex then I
highly recommend this online regex builder:

http://regex101.com/#pcre

The thing I like about it is that you can choose the 'Flavor' of regex
it uses, so unlike other online regex builders that use Javascript,
you can choose the same library that Livecode uses: PCRE. So if it
works on this site it should work in LC.

Even better, if you click on the Community tab at the top of the page
it will present you with a list of 100s and 100s of community provided
examples of regex, explains what they do, and it's as simple as copy
and paste into your matchChunk or matchText function to test in LC.

I will warn you though of one gotcha that is easy to trip on if you're
an occasional user of regex + LC. The standard matchText function
looks like this:

matchTex(yourDataVar,"regex(capturethis)expression",yourCapturedVar)

so any matches within () will be placed in your variable place holder.

In regex the | character is used as OR

so you might be looking for lowercase jpg file names, but you want
just the name, not the extension:

([a-z]+)\.jpg  will work, but not if you also have some with a jpeg extension.

([a-z]+.jpeg will work for those, so these could be combined with the
| identifier:

([a-z]+)\.jpg|([a-z]+)\.jpeg

Now I appreciate the regex experts will point out there is a much
better way to solve this problem than using |, but the point of this
is, if you use | in your regex, and place it in matchText or
matchChunk, you no longer have a single placeholder, so your matchText
function has to be amended accordingly:

matchText(yourDataVar,"([a-z]+)\.jpg|([a-z]+)\.jpeg",myJpgFile,myJpegFile)

It is easy, when copying regex off such sites listed above, or
building your own, and you know you are only finding x number of data
bites, to assume that that is how many capture variables you need, but
if OR is used, then you can easily end up needing double or triple the
number of variables.

Rule of thumb, always count the number of capturing () in the regex
and ensure you have the same number of allocated capturing variables.

HTH




More information about the use-livecode mailing list