Need regex lesson

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Need regex lesson

Post by dunbarx » Mon Dec 29, 2014 10:32 pm

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".

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Need regex lesson

Post by MaxV » Tue Dec 30, 2014 4:48 pm

The correct code is:

Code: Select all

on mouseUp
put replaceText(fld 3,"\/*\/","//") into fld 4
end mouseUp
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Need regex lesson

Post by dunbarx » Tue Dec 30, 2014 5:19 pm

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

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Need regex lesson

Post by MaxV » Mon Jan 05, 2015 3:36 pm

Hi Craig,
try this site: http://www.regexr.com/
It saved my life many times about regular expressions. :D
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Need regex lesson

Post by FourthWorld » Mon Jan 05, 2015 8:24 pm

Another useful resource is the RegEx Builder plugin included in the LiveCode installation - in the IDE see Development->Plugins->RegEx Builder.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Need regex lesson

Post by dunbarx » Mon Jan 05, 2015 9:37 pm

Richard.

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

The great...

Craig

Post Reply