How to remove the coutrycode from telphonumber.
the number could come in this format.
4770300000
004770300000
+4770300000
4670300000
I would like to remove countrycodes to be abile to match does number against.
070300000
The following should replace any valid country code (starting with "00" or "+") with a leading zero.
resultString = Regex.Replace(subjectString, @"^(00|\+)(1|2[078]|2[1234569][0-9]|3[0123469]|3[578][0-9]|4[013456789]|42[0-9]|5[09][0-9]|5[12345678]|6[0123456]|6[789][0-9]|7|8[0578][0-9]|8[123469]|9[0123458]|9[679][0-9])", "0");
Do you have a list of expected country codes? If so (assuming for this example that you're just looking for 47 and 46):
resultString = Regex.Replace(subjectString, @"(\+|\b00|\b)4[67]", "0");
would change
4770300000 004770300000 +4770300000 4670300000
into
070300000 070300000 070300000 070300000
However, it would also trigger on a phone number where the country code and area code have been omitted, if it happens to start with 47 or 46. To guard against this, you might want to add a lookahead assertion that checks that at least 8 (or whatever is reasonable) digits follow the presumed country code.
So
resultString = Regex.Replace(subjectString, @"(\+|\b00|\b)4[67](?=\d{8})", "0");
would change
4770300000 004770300000 +4770300000 460000
into
070300000 070300000 070300000 460000
If your input string is supposed to consist entirely of a phone number (as opposed to phone numbers embedded somewhere in a longer text), then you might want to use
resultString = Regex.Replace(subjectString, @"^(\+|00)4[67](?=\d{8})", "0");
instead. Thanks to Michael for the suggestion!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With