Page 1 of 1

Regexes for replaceText

Posted: Tue Oct 22, 2013 9:49 am
by garyth123
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.

Re: Regexes for replaceText

Posted: Tue Oct 22, 2013 10:30 am
by garyth123
Oh, I see the reference is http://perldoc.perl.org/perlre.html

Re: Regexes for replaceText

Posted: Tue Oct 22, 2013 11:12 am
by garyth123
I'm trying

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

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

Re: Regexes for replaceText

Posted: Tue Oct 22, 2013 1:00 pm
by Klaus
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

Re: Regexes for replaceText

Posted: Tue Oct 22, 2013 1:01 pm
by garyth123
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!

Re: Regexes for replaceText

Posted: Tue Oct 22, 2013 1:04 pm
by Klaus
Sorry, "regex" are still a complete mystery to me :D

Re: Regexes for replaceText

Posted: Tue Oct 22, 2013 2:37 pm
by garyth123
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

Re: Regexes for replaceText

Posted: Tue Oct 22, 2013 3:39 pm
by dunbarx
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

Re: Regexes for replaceText

Posted: Tue Oct 22, 2013 4:20 pm
by garyth123
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