Reputation: 77
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.
Upvotes: 3
Views: 4108
Reputation: 1
Consider the Sri Lankan ISP's mobile operator codes when designing the pattern.
ex:
070: SLTMobitel
072: Hutch
074: Dialog
Regex:
const regex = /^(?:7)[01245678]{1}[0-9]{7}|(0|(?:\+94))[7]{1}[01245678]{1}[0-9]{7}$/gm;
1st part of the pattern for 9 digits number.
2nd part of the pattern for 10 digits number.
Upvotes: 0
Reputation: 11
Simple Regex to test for mobile and landline Sri Lankan numbers:
^(\+94|0)?[1-9]{2}[0-9]{7}$ - "test for landline numbers"
^(\+94|0)?7[0-9]{8}$ - "test for mobile numbers"
(^(\+94|0)?[1-9]{2}[0-9]{7}$)|(^(\+94|0)?7[0-9]{8}$) - "combined regex"
All telephone numbers in Sri Lanka follow a general format. 0 or +94 followed by 9 digits. Some extra rules apply depending on whether the number is a landline or mobile.
+94
, 0
, or not be specified.1
to 9
0
to 9
.+94
, 0
, or not be specified.This was made following Sri Lankan telephone formatting conventions. Testing individual area codes in landlines was ignored. Testing mobile operator codes was also unnecessary since they go from 070 to 079 (although 079 is unallocated currently).
Sources:
National Conventions for writing telephone numbers
Telephone Numbers in Sri Lanka
Upvotes: 1
Reputation: 21
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}$";
Upvotes: 0
Reputation: 186793
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
Upvotes: 14
Reputation: 270
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)
Upvotes: 0