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.
Solved! Go to Solution.
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')
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')
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.
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?.
User | Count |
---|---|
89 | |
37 | |
26 | |
13 | |
12 |
User | Count |
---|---|
128 | |
53 | |
38 | |
26 | |
21 |