Sam
Sam

Reputation: 77

c# Regex to match all Sri Lankan phone numbers

I didn't find any on google on the regex expression for Sri Lankan phone numbers; The formats are:

  1. 775645645 (9 digits)
  2. 0775645645 (10 digits)
  3. +94775645645

It should start with 7 or 0 or +94. Can anyone help me with this. Appreciate your solutions.

Upvotes: 3

Views: 4108

Answers (5)

Pulasthi Buddika
Pulasthi Buddika

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

Nithika Dias
Nithika Dias

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"

Explanation

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.

Landlines

  • The number can start with either +94, 0, or not be specified.
  • Followed by a 2-digit area code, digits that can be included are from 1 to 9
  • Finally end with the operator code, followed by the 6 digits of the specified landline that all can contain digits 0 to 9.

Mobile Numbers

  • The number can start with either +94, 0, or not be specified.
  • Followed by a 2-digit operator code (currently all operator codes start with 7).
  • Finally end the with the 7 digits of the specified mobile.

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

Hasitha Nilupul
Hasitha Nilupul

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

Dmitrii Bychenko
Dmitrii Bychenko

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

damith alahakoon
damith alahakoon

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

Related Questions