Using Replace with an undefined number in a string

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

Using Replace with an undefined number in a string

Post by andrewferguson » Thu Oct 10, 2013 7:16 pm

Hi,
I am trying to use a replace command within a large number of text files. What I am trying to do is replace text that contains some words, then a number, and then more words. All the words will be the same, but the number could be different.

Code: Select all

put "hello1234beep some text" into theText
replace "hello" & <The Number> & "beep" with "more text" in theText
The number could be any integer, and could have any number of digits.
Also, I will not know the position of the string to replace within the text file. (Otherwise I could just replace word 1, and word 2...)

Any idea how to do what I want?
Thanks,
Andrew

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Using Replace with an undefined number in a string

Post by jacque » Thu Oct 10, 2013 7:46 pm

Sounds like a job for the "replaceText" command. That takes a regex expression as the text to search for.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using Replace with an undefined number in a string

Post by Klaus » Thu Oct 10, 2013 8:53 pm

Hi Andrew,

since my knowledge about REGEX is erm. limited, "replace" would be my choice :D
...
put 1234 into tNumber
put "hello1234beep some text" into theText
replace ("hello" & tNumber & "beep") with "more text" in theText
...

HInt:
"replace" will replace ALL occurrences of the "text to be replaced" in a variable or field,
so no need to know where they exactly are!

Is that what you are looking for?


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Using Replace with an undefined number in a string

Post by jacque » Thu Oct 10, 2013 9:28 pm

Klaus, that was my first response too, and then I deleted it and rewrote it to suggest replaceText. The reason is that the OP says the number will always be different, so I assume he's searching for any number inside the text string. If I'm wrong, then I'd prefer "replace" too.

Since my regex is weak too, I didn't have time to go look it up (I think it's pretty simple) but I bet someone else will know it. If I get some time I'll try to remember to look it up. I'm hoping Andrew knows it. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using Replace with an undefined number in a string

Post by Klaus » Thu Oct 10, 2013 10:06 pm

AHA! :D

Looks like I completely misunderstood the question...

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

Re: Using Replace with an undefined number in a string

Post by Thierry » Fri Oct 11, 2013 2:09 am

andrewferguson wrote:

Code: Select all

put "hello1234beep some text" into theText
replace "hello" & <The Number> & "beep" with "more text" in theText
The number could be any integer, and could have any number of digits.
You can try this one if your words are fix.
NB: I guess there is no space in between words and the number

Code: Select all

put replaceText( yourtext, "hello\d+beep", "done") 

HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Using Replace with an undefined number in a string

Post by jacque » Fri Oct 11, 2013 3:08 am

I knew you'd have the answer, Thierry. :-)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Using Replace with an undefined number in a string

Post by Thierry » Fri Oct 11, 2013 3:43 am

jacque wrote:I knew you'd have the answer, Thierry. :-)
A votre service, Madame :)
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply