search and replace number in a string

Hi all,

I might not be able to see the wood for the trees here but I have a situation where I need to extract numbers from a string, convert them and then replace them in the string.

here are some string variations:

339-9
339+9
some text 339-9
some text 339+9

I’m currently using this to extract the numbers from the string:

			set numbers_list to every paragraph of (do shell script "echo \"" & theString & "\" | egrep \"[0-9]{1,}\" -o")

I then repeat through the numbers_list of numbers and convert them to the new value. All of this is working fine but setting a new string with the converted numbers is causing a problem. I tried using the number as a text offset i.e

the offset of 339 in 339-9 is 1 which is correct but the offset of 9 in 339-9 is 3 not 5 because the 9 matches the first occurrence of 9.

Apologies if I’ve not explained this very well but it has me stumped.

I’m currently trying to use sed to do the replacement:

echo “339-9” | sed -E ‘s/[-|+]9/10/g’

but this just gives me 33910 losing the wildcard of either - or +

Thanks,
Nik

Does this do something like you want?

set theString to "339-9" as text
set newString to setNewNumString(theString, "442")

on setNewNumString(originalString, newValue)
	set numbers_list to every paragraph of (do shell script "echo \"" & originalString & "\" | egrep \"[0-9]{1,}\" -o")
	set firstNumString to item 1 of numbers_list as text
	set theWildCard to character ((length of firstNumString) + 1) of originalString as text
	set theResult to newValue & theWildCard & item 2 of numbers_list as text
	return theResult
end setNewNumString

This code would need to be modified with a repeat loop if there are more number strings separated by what you term a wild card than just two.

Hope this helps…:slight_smile:

Hi,

Thanks for your response but when I use the string “some text 339-9” it just returns “442e9”

I was hoping that sed was going to be able to do the search and replace but it keeps replacing the wildcard.

Thanks again,
Nik

Sorry, I missed the need to extract from a string. Here is the code modified to handle the case needed to find the wild card when embedded in a string.

See if this helps:

set theString to "some text 339-9" as text
set newString to setNewNumString(theString, "442")

on setNewNumString(originalString, newValue)
	set numbers_list to every paragraph of (do shell script "echo \"" & originalString & "\" | egrep \"[0-9]{1,}\" -o")
	set firstNumString to item 1 of numbers_list as text
	display dialog firstNumString & "   " & item 2 of numbers_list as text
	set theWildCard to character ((offset of firstNumString in originalString) + (length of firstNumString)) of originalString as text
	display dialog theWildCard
	set theResult to newValue & theWildCard & item 2 of numbers_list as text
	return theResult
end setNewNumString

Sorry again. The display dialog debug lines have been removed in the following:

set theString to "some text 339-9" as text
set newString to setNewNumString(theString, "442")

on setNewNumString(originalString, newValue)
	set numbers_list to every paragraph of (do shell script "echo \"" & originalString & "\" | egrep \"[0-9]{1,}\" -o")
	set firstNumString to item 1 of numbers_list as text
	set theWildCard to character ((offset of firstNumString in originalString) + (length of firstNumString)) of originalString as text
	set theResult to newValue & theWildCard & item 2 of numbers_list as text
	return theResult
end setNewNumString

Hi,

the script works fine on the 339 value but when I need to replace the value 9 it doesn’t. This maybe down to me not explaining myself properly. I need to extract the numbers from the string, in this case 339 & 9 and then multiply them by like so:

set theString to "some text 339-9" as text
set conVar to 1.0936133
set numbers_list to every paragraph of (do shell script "echo \"" & theString & "\" | egrep \"[0-9]{1,}\" -o")

repeat with thisNumber in numbers_list
	set convertedNumber to round (thisNumber * conVar) rounding as taught in school
end repeat

Once I have the converted numbers I need to find the original values in the string and replace them with the calculated values so if the original string is this:

“some text 339-9”

the converted string needs to look like this:

“some text 371-10”

Hope this explains the scenario better and thanks for your help,
Nik

Ok, I think I’ve come up with something that will do the trick but I would still appreciate any other comments.

set theString to "some text 339-9" as text
set conVar to 1.0936133
set numbers_list to every paragraph of (do shell script "echo \"" & theString & "\" | egrep \"[0-9]{1,}\" -o")

if (count of numbers_list) = 2 then
	set convertedNumber1 to round ((item 1 of numbers_list) * conVar) rounding as taught in school
	set convertedString to do shell script "echo \"" & theString & "\" | sed -e 's/" & (item 1 of numbers_list) & "/" & convertedNumber1 & "/g'"
	set convertedNumber2 to round ((item 2 of numbers_list) * conVar) rounding as taught in school
	set convertedString to do shell script "echo \"" & convertedString & "\" | sed -e 's/[-]" & (item 2 of numbers_list) & "/-" & convertedNumber2 & "/g' -e 's/[+]" & (item 2 of numbers_list) & "/+" & convertedNumber2 & "/g'"
end if

Thanks,
Nik