is there a way to get text between 2 characters
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
is there a way to get text between 2 characters
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"
For example.. put text between "for" and "be" into .... would give me "any text that might"
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här
Re: is there a way to get text between 2 characters
Hi DavJans,DavJans wrote: put text between "for" and "be" into .... would give me "any text that might"
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
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: is there a way to get text between 2 characters
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
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
Re: is there a way to get text between 2 characters
This is great, thank you. I have found a problem though.
here is an example.
this returns "Sorry, find nothing!", I'm guessing it ignored <Stuff>?
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
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här
Re: is there a way to get text between 2 characters
Hi DavJans,DavJans wrote: This is great, thank you. I have found a problem though.
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
Code: Select all
put "\Q" & w0 & "\E" & "(.*?)" & "\Q" & w1 & "\E" into Rx
Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: is there a way to get text between 2 characters
Great, thank you so very much.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här