Find and manipulate number in a string

I would like to make Alexa say “one two three four fife Sometown” out of “12345 Sometown” instead “onethousandthreehundredfourtyfive Sometown” with the AmazonEchoControl Binding (TTS).

Therefore I do have the string “12345 Sometown” and want to transfer it to “1 2 3 4 5 Sometown”. Tried several Regex expressions but that was not successful. Any ideas welcome.

Use “one two three four five Sometown”

1 Like

my fault, I have not been precise enough. What I do is:

  1. get location data for iPhones tracked via the iCloud Binding
  2. get adress of this location data from Google Maps Service (Output: “Somestreet 123, 12345 Sometown”)
  3. let Alexa say the adress via TTS (AlexaControlBinding).

Right now Alexa says “Somestreet onehundredtwentythree, twelvethousandthreehundredfourtyfive Sometown” but I want it to to be “Somestreet onehundredtwentythree, one two three four five Sometown”.

In terms of code, I created a testrule doing something like this:

var String teststring = “Somestreet2 123, 12345 Sometown”
val String teststring1 = new String(transform(“REGEX”, “[0-9]{4,5} .*”, teststring))
logWarn(“Test”, "Extract: " + teststring1) Blockquote

still gives me “Somestreet2 123, 12345 Sometown”. I know the regex is wrong but have no clue how to change it so that I get

“Somestreet2 123, 1 2 3 4 5 Sometown”
or alternatively
“Somestreet2 123, one two three four five Sometown”

If you have the value of teststring1 successfully returned as “12345”, you can then just split it up into single-character pieces. Possibly by this method.

@namraccr, thanks.

Solved it with regex only:

val inString = "Somestreet 123, 12345 Sometown"
logWarn("Test", "INPUT: " + inString) 
val zipCode = transform("REGEX", ".*([1-9]\\d{4,}).*", inString)
val strPart1 = transform("REGEX", "(.*) " + zipCode + ".*", inString)
val strPart2 = transform("REGEX", ".*" + zipCode + "(.*).*", inString)
val zipCodeNew = transform("REGEX", "s/([0-9])/ $1/g", zipCode)
val outString = strPart1 + zipCodeNew + strPart2
logWarn("Test", "OUTPUT: " + outString) 

Output is

OUTPUT: Somestreet 123, 1 2 3 4 5 Sometown