Regexes for replaceText
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Regexes for replaceText
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.
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
Oh, I see the reference is http://perldoc.perl.org/perlre.html
Re: Regexes for replaceText
I'm trying
matchText(theAnswer, "(\w+)\s+\(\w+\)",theShortAnswer)
but getting an execution error Handler: can't find handler near matchText, char1
matchText(theAnswer, "(\w+)\s+\(\w+\)",theShortAnswer)
but getting an execution error Handler: can't find handler near matchText, char1
Re: Regexes for replaceText
Hi Garyth,
"matchtext()" is a FUNCTION not a HANDLER, so you need to PUT it INTO a variable or something
...
put matchText(theAnswer, "(\w+)\s+\(\w+\)",theShortAnswer) into tMatchTextResult
...
Best
Klaus
"matchtext()" is a FUNCTION not a HANDLER, so you need to PUT it INTO a variable or something

...
put matchText(theAnswer, "(\w+)\s+\(\w+\)",theShortAnswer) into tMatchTextResult
...
Best
Klaus
Re: Regexes for replaceText
Okay, I'm now trying
for the first of the above mentioned tasks. However when I compare myAnswer to theShortAnswer1 or theShortAnswer2 they do not match.
And
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!
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
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
Sorry, "regex" are still a complete mystery to me 

Re: Regexes for replaceText
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![]()
...
put matchText(theAnswer, "(\w+)\s+\(\w+\)",theShortAnswer) into tMatchTextResult
...
Best
Klaus
Re: Regexes for replaceText
Hi.
Are you working too hard on this? Would not:
do what you need? You can also do this with the "offset" function.
Am I missing it?
Craig Newman
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
Am I missing it?
Craig Newman
Re: Regexes for replaceText
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.
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