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