Suppose I have a phone number stored in the string "0991234567".
What kind of manipulations with regular expressions can bring this line to the form "+450 (99) 123 45 67"?
Suppose I have a phone number stored in the string "0991234567".
What kind of manipulations with regular expressions can bring this line to the form "+450 (99) 123 45 67"?
If the city and country codes are always the same (taking into account the comments) then regular expressions are not even needed, the combination substring and concat will do everything.
If you really want to do this with regular expressions, then in the simplest case, I would write this:
string phone = "0991234567"; phone.replaceAll("(\\d)(\\d{2,3})(\\d{3})(\\d{2})(\\d{2})", "+45$1 ($2) $3 $4 $5"); Once again, I note that the analysis of an arbitrary number is not done in this way. The number of digits in the city, country code, the number of digits in the number itself may be different.
Source: https://ru.stackoverflow.com/questions/446144/
All Articles