cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Kaptea
Helper I
Helper I

Trying to Match a sting with mutiple values

Hi,

I am trying to use the replace method to achieve the following:

 

We need to look at the result of a string say called "toCountry" this "toCountry" could be multiple values, for example, +353 or +44 or +1 or +34, it will always be a single value but could be any of these. I want to match the output of "toCountry" and then replace it with text.

 

For Example:

 

"toCountry" = +353 then replace +353 with IE.

replace(variables('toCountry'), '+353', 'IE')

 

or

 

"toCountry" = +44 then replace +44 with UK.

replace(variables('toCountry'), '+44', 'UK')

 

I am trying to do this dynamically using one expression instead of multiple, initialize and compose blocks.

Any help would be greatly appreciated.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
tom_riha
Super User
Super User

Hello @Kaptea ,

you can combine multiple replace(...) expressions together by taking the output from the previous replace and applying another one on it.

replace(replace(replace(replace(variables('toCountry'), '+353', 'IE'), '+44', 'UK'), '+XX', 'YY'), '+YY', 'AA')

 



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]

View solution in original post

4 REPLIES 4
tom_riha
Super User
Super User

Hello @Kaptea ,

you can combine multiple replace(...) expressions together by taking the output from the previous replace and applying another one on it.

replace(replace(replace(replace(variables('toCountry'), '+353', 'IE'), '+44', 'UK'), '+XX', 'YY'), '+YY', 'AA')

 



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]

Hi @tom_riha thanks for your reply this worked perfect. Is there a way to work in a starts with part of the expression, right now we truncating the ToCountry to 4 digits say +353, but the number could be +162 for example and in the second case we just need to match the +1 which is 2 digits to match the US, so we need to cycle through the digits and then if it matches any of the set replaces, replace that text with the country code.

Hello @Kaptea ,

the replace(...) expressions will be evaluated from the inside out, in the example above:

replace(replace(replace(replace(variables('toCountry'), '+353', 'IE'), '+44', 'UK'), '+XX', 'YY'), '+YY', 'AA')

it'll check for +353 first, then for +44, then +XX, etc. It doesn't matter how many numbers it's looking for, it'll always take the first match and replace it. And since the phone number will contain + only once, it'll be the only replace the expression can do.



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]

Hi @tom_riha once again thanks for your help here, the issue I am facing is, the toCountry is always set to the read the first 4 digits of the provided phone number, so it will always have 4 digits, so when we match say against the UK it matches the +44 perfect but because its 4 digits it then adds a number to the end, so ends up like UK7 instead of just UK. Do you know of any workaround for this use case?.

Helpful resources

Top Solution Authors
Users online (4,185)