Page 1 of 1

Using Replace with an undefined number in a string

Posted: Thu Oct 10, 2013 7:16 pm
by andrewferguson
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

Re: Using Replace with an undefined number in a string

Posted: Thu Oct 10, 2013 7:46 pm
by jacque
Sounds like a job for the "replaceText" command. That takes a regex expression as the text to search for.

Re: Using Replace with an undefined number in a string

Posted: Thu Oct 10, 2013 8:53 pm
by Klaus
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

Re: Using Replace with an undefined number in a string

Posted: Thu Oct 10, 2013 9:28 pm
by jacque
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. :)

Re: Using Replace with an undefined number in a string

Posted: Thu Oct 10, 2013 10:06 pm
by Klaus
AHA! :D

Looks like I completely misunderstood the question...

Re: Using Replace with an undefined number in a string

Posted: Fri Oct 11, 2013 2:09 am
by Thierry
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

Re: Using Replace with an undefined number in a string

Posted: Fri Oct 11, 2013 3:08 am
by jacque
I knew you'd have the answer, Thierry. :-)

Re: Using Replace with an undefined number in a string

Posted: Fri Oct 11, 2013 3:43 am
by Thierry
jacque wrote:I knew you'd have the answer, Thierry. :-)
A votre service, Madame :)