is there a way to get text between 2 characters

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
DavJans
Posts: 275
Joined: Thu Dec 12, 2013 4:21 pm

is there a way to get text between 2 characters

Post by DavJans » Thu Mar 02, 2017 12:32 am

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"
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: is there a way to get text between 2 characters

Post by Thierry » Thu Mar 02, 2017 8:09 am

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: is there a way to get text between 2 characters

Post by AxWald » Thu Mar 02, 2017 12:15 pm

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!
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!

DavJans
Posts: 275
Joined: Thu Dec 12, 2013 4:21 pm

Re: is there a way to get text between 2 characters

Post by DavJans » Thu Mar 02, 2017 5:10 pm

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>?
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: is there a way to get text between 2 characters

Post by Thierry » Thu Mar 02, 2017 5:25 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

DavJans
Posts: 275
Joined: Thu Dec 12, 2013 4:21 pm

Re: is there a way to get text between 2 characters

Post by DavJans » Thu Mar 02, 2017 5:34 pm

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

Post Reply