Regexes for replaceText

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Regexes for replaceText

Post by garyth123 » Tue Oct 22, 2013 9:49 am

Hi,

I need to find a regular expression that will allow me to find two chunks of text separated by a "/", so eg ""piece of text 1/piece of text 2". I want to place "piece of text 1" into one variable, and "piece of text 2" into another. And another regex which will allow me to find text such as: "piece of text (some more text which I wish to discard including the brackets)" leaving just "piece of text".

It's been a long time since I did any regular expressions (which I used to enjoy). Where can I find out more info on the regexes that replaceText uses? Or are there simpler ways of doing what I want?

Thank you.

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Regexes for replaceText

Post by garyth123 » Tue Oct 22, 2013 10:30 am

Oh, I see the reference is http://perldoc.perl.org/perlre.html

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Regexes for replaceText

Post by garyth123 » Tue Oct 22, 2013 11:12 am

I'm trying

matchText(theAnswer, "(\w+)\s+\(\w+\)",theShortAnswer)

but getting an execution error Handler: can't find handler near matchText, char1

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Regexes for replaceText

Post by Klaus » Tue Oct 22, 2013 1:00 pm

Hi Garyth,

"matchtext()" is a FUNCTION not a HANDLER, so you need to PUT it INTO a variable or something 8)
...
put matchText(theAnswer, "(\w+)\s+\(\w+\)",theShortAnswer) into tMatchTextResult
...

Best

Klaus

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Regexes for replaceText

Post by garyth123 » Tue Oct 22, 2013 1:01 pm

Okay, I'm now trying

Code: Select all

     set itemdel to "/"
      put item 1 of theAnswer & " " into theShortAnswer1
      put item 1 to -1 of theShortAnswer1 into theShortAnswer1
      put item 2 of theAnswer into theShortAnswer2
      put item 1 to -1 of theShortAnswer2 into theShortAnswer2
for the first of the above mentioned tasks. However when I compare myAnswer to theShortAnswer1 or theShortAnswer2 they do not match.

And

Code: Select all

      put word 1 of theAnswer & " " into theShortAnswer
      put word 2 of theAnswer after theShortAnswer
      put theShortAnswer into theAnswer


as mostly (but not always) there are two words preceding the parentheses and the text I wish to discard. This works but sometimes it will be three words preceding the parentheses.

There must be a better way to do this!

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Regexes for replaceText

Post by Klaus » Tue Oct 22, 2013 1:04 pm

Sorry, "regex" are still a complete mystery to me :D

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Regexes for replaceText

Post by garyth123 » Tue Oct 22, 2013 2:37 pm

Okay Klaus, thanks for clearing up that general LC point.
Klaus wrote:Hi Garyth,

"matchtext()" is a FUNCTION not a HANDLER, so you need to PUT it INTO a variable or something 8)
...
put matchText(theAnswer, "(\w+)\s+\(\w+\)",theShortAnswer) into tMatchTextResult
...

Best

Klaus

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

Re: Regexes for replaceText

Post by dunbarx » Tue Oct 22, 2013 3:39 pm

Hi.

Are you working too hard on this? Would not:

Code: Select all

on mouseUp
   find  string  "xxx/yyy"  in fld "yourField"
   get the foundText --or the foundChunk or whatever find thing works for you...
   set the itemDelimiter to "/"
   put item 1 of it into something
   put item 2 of it into somethingElse
end mouseUp
do what you need? You can also do this with the "offset" function.

Am I missing it?

Craig Newman

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Regexes for replaceText

Post by garyth123 » Tue Oct 22, 2013 4:20 pm

Hi Craig,

Thanks for this. I have managed to get something working pretty good with a regex which needs a little tweaking.

However I will investigate if using foundText etc will do as well.
dunbarx wrote:

Code: Select all

on mouseUp
   find  string  "xxx/yyy"  in fld "yourField"
   get the foundText --or the foundChunk or whatever find thing works for you...
   set the itemDelimiter to "/"
   put item 1 of it into something
   put item 2 of it into somethingElse
end mouseUp

Post Reply