I didn't find any on google on the regex expression for Sri Lankan phone numbers; The formats are:
775645645
(9 digits)0775645645
(10 digits)+94775645645
It should start with 7 or 0 or +94. Can anyone help me with this. Appreciate your solutions.
I didn't find any on google on the regex expression for Sri Lankan phone numbers; The formats are:
775645645
(9 digits)0775645645
(10 digits)+94775645645
It should start with 7 or 0 or +94. Can anyone help me with this. Appreciate your solutions.
Let's build the pattern
:
^ - anchor (string start)
7|0|(?:\+94) - either 7 or 0 or +94
[0-9]{9,10} - from 9 up and including to 10 digits (chars on [0-9] range)
$ - anchor (string end)
So we have
string pattern = @"^(?:7|0|(?:\+94))[0-9]{9,10}$";
Tests:
string[] tests = new string[] {
"775645645",
"0775645645",
"+94775645645",
"123456669",
"1234566698888",
"+941234566698888",
"+94123456"
};
string pattern = @"^(?:7|0|(?:\+94))[0-9]{9,10}$";
string report = string.Join(Environment.NewLine, tests
.Select(item => $"{item,-20} : {(Regex.IsMatch(item, pattern) ? "Matched" : "Not")}"));
Console.Write(report);
Outcome:
775645645 : Matched
0775645645 : Matched
+94775645645 : Matched
123456669 : Not
1234566698888 : Not
+941234566698888 : Not
+94123456 : Not
String regexPhoneNumber = "^(?:0|94|\\+94)?(?:(11|21|23|24|25|26|27|31|32|33|34|35|36|37|38|41|45|47|51|52|54|55|57|63|65|66|67|81|912)(0|2|3|4|5|7|9)|7(0|1|2|5|6|7|8)\\d)\\d{6}$";
Valid phone number Types:-
0771234567
771234567
+94771234567
94771234567
0111234567(local codes)
Updated to newly assigned mobile providers:
String regexPhoneNumber = "^(?:0|94|\\+94)?(?:(11|21|23|24|25|26|27|31|32|33|34|35|36|37|38|41|45|47|51|52|54|55|57|63|65|66|67|81|912)(0|2|3|4|5|7|9)|7(0|1|2|4|5|6|7|8)\\d)\\d{6}$";