Page 1 of 1

Need regex lesson

Posted: Mon Dec 29, 2014 10:32 pm
by dunbarx
I gave this the other day as an example of how to replace any number or length of strings of "/" with "//":

Code: Select all

on mouseUp
put replaceText(fld 3,"/*/","//") into fld 4
end mouseUp
So if you had:

"a/a//a///a//////////////a"

You would get "a//a//a//a//a". Works like a charm.

But I all of a sudden do not understand my own gadget. In other words, if I am running this handler on, say, "a/a/", shouldn't I get: "a//". The function should match the wildcard string "/...../" (the string fragment "/a/", char 2 to 4) and return "//" in its place. That is, the first "a" appended to "//". At least that is how I read the explanation of the wildcard "*" in the dictionary entry of the "filter" command. Is replaceText different? I doubt it.

Mr Thierry?

Craig Newman

EDIT, I had thought that the "/" character itself was an issue, since it is an escape char in PCRE expressions, but it makes no difference if you substitute, say, "x". "AxAxxAxxxxxxxA" yields "AxxAxxAxxA".

Re: Need regex lesson

Posted: Tue Dec 30, 2014 4:48 pm
by MaxV
The correct code is:

Code: Select all

on mouseUp
put replaceText(fld 3,"\/*\/","//") into fld 4
end mouseUp

Re: Need regex lesson

Posted: Tue Dec 30, 2014 5:19 pm
by dunbarx
MaxV.
put replaceText(fld 3,"\/*\/","//") into fld 4
That works as well. So is mine deficient in syntax, though it works fine in practice?

After reading up a bit on regular expressions, I see that the implementation of "*" really refers to "0 or more of the preceding char", and not so much, as the dictionary says, "0 or more of any char". I think this clears up what I was wondering about. The dictionary entry (under "filter") has mislead me for quite a while.

Craig

Re: Need regex lesson

Posted: Mon Jan 05, 2015 3:36 pm
by MaxV
Hi Craig,
try this site: http://www.regexr.com/
It saved my life many times about regular expressions. :D

Re: Need regex lesson

Posted: Mon Jan 05, 2015 8:24 pm
by FourthWorld
Another useful resource is the RegEx Builder plugin included in the LiveCode installation - in the IDE see Development->Plugins->RegEx Builder.

Re: Need regex lesson

Posted: Mon Jan 05, 2015 9:37 pm
by dunbarx
Richard.

And I just discovered that this was authored by none other than Frederic Rinaldi.

The great...

Craig