Page 1 of 1
is there a way to get text between 2 characters
Posted: Thu Mar 02, 2017 12:32 am
by DavJans
I'm trying to search a text field line by line looking for any text that might be between something, anything really.
For example.. put text between "for" and "be" into .... would give me "any text that might"
Re: is there a way to get text between 2 characters
Posted: Thu Mar 02, 2017 8:09 am
by Thierry
DavJans wrote: put text between "for" and "be" into .... would give me "any text that might"
Hi DavJans,
Ok, here is a small snippet as a proof of concept.
Code: Select all
put "I'm trying to search a text field line by line looking " & \
"for any text that might be between something, anything really." into T
answer getTextFrom( T , "between", "for", "be")
Code: Select all
function getTextFrom T, S, w0, w1
local Rx, textFound
put "\s" & w0 & "\s" & "(.*?)" & "\s" & w1 & "\s" into Rx
if matchText(T, Rx, textFound) then
return textFound
else
return "Sorry, find nothing!"
end if
end getTextFrom
if you are part of those who feel sick with regex,
you can replace the matchText() line with a couple of offset() livecode functions.
This is left as an exercice to the reader
And from the dictionary:
Code: Select all
truewordOffset("Chile",tListOfCountries) -- returns 48
truewordOffset("d'être","Ce n'est pas tant d'être riche qui fait le bonheur, c'est de le devenir.") -- returns 5
HTH,
Thierry
Re: is there a way to get text between 2 characters
Posted: Thu Mar 02, 2017 12:15 pm
by AxWald
Code: Select all
get "I'm trying to search a text field line by line looking for " & \
"any text that might be between something, anything really."
put "for " into myStart
put " be" into myEnd
get char (offset(myStart,it) + len(myStart)) to \
(offset(myEnd,it,(offset(myStart,it) + len(myStart))) \
+ (offset(myStart,it) + len(myStart))-1) \
of it
if it is empty then
answer error "No matches!"
else
put it
end if
Make sure myEnd actually comes behind myStart! ;-)
Have fun!
Re: is there a way to get text between 2 characters
Posted: Thu Mar 02, 2017 5:10 pm
by DavJans
This is great, thank you. I have found a problem though.
here is an example.
Code: Select all
on mouseUp
put "<Value>12345</Value>" into T
put getTextFrom( T , "between", "<Value>", "</Value>") & cr after fld "Field 1"
end mouseUp
function getTextFrom T, S, w0, w1
local Rx, textFound
put "\s" & w0 & "\s" & "(.*?)" & "\s" & w1 & "\s" into Rx
if matchText(T, Rx, textFound) then
return textFound
else
return "Sorry, find nothing!"
end if
end getTextFrom
this returns "Sorry, find nothing!", I'm guessing it ignored <Stuff>?
Re: is there a way to get text between 2 characters
Posted: Thu Mar 02, 2017 5:25 pm
by Thierry
DavJans wrote:
This is great, thank you. I have found a problem though.
Hi DavJans,
Mmm, I'm sure you'll going to find more than one
I gave you a basic and very naive way to start doing what you want.
From your text sample, in your 1st post,
I guessed you were looking for text in between 2 words.
Your last example is completely different, therefore can't work.
Replace:
Code: Select all
put "\s" & w0 & "\s" & "(.*?)" & "\s" & w1 & "\s" into Rx
with:
Code: Select all
put "\Q" & w0 & "\E" & "(.*?)" & "\Q" & w1 & "\E" into Rx
and this user's case will work.
Regards,
Thierry
Re: is there a way to get text between 2 characters
Posted: Thu Mar 02, 2017 5:34 pm
by DavJans
Great, thank you so very much.