1

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.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Sam
  • 57
  • 1
  • 5

3 Answers3

13

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
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • i tried to use this for angular +94775645645 this give me not matching state. if i deduct one number it show as valid. i think language is not matter regex is same for everyware right? here is my code "Validators.pattern('^7|0|(?:\\+94)[0-9]{9,10}$')" – Sahan Pasindu Nirmal Sep 18 '18 at 15:23
  • @Sahan Pasindu Nirmal: could you, please, turn the *comment* to a *question*? Mark it with *Angular* tag and provide not just `Validators.pattern('^7|0|(?:\\+94)[0-9]{9,10}$')` but the context (how do you apply the validator on the input data)? – Dmitry Bychenko Sep 18 '18 at 19:28
  • in angular we can specify pattern validation using this method "Validators.pattern" full code like this this.registrationForm = this.fb.group({ userName: [ null, Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required]) ], – Sahan Pasindu Nirmal Sep 19 '18 at 01:16
  • This is a example i can place any patter which i want. but if i use +94712345678 like this that give me error like pattern not valid, how ever when i enter with +947 and more seven number that give me it is valid number, but it is wrong it must be contain +947 with more 8 numbers. – Sahan Pasindu Nirmal Sep 19 '18 at 01:19
  • i cannot explain this within a comment section in here, any place we can talk in more details? thank you :) – Sahan Pasindu Nirmal Sep 19 '18 at 01:20
  • @Sahan Pasindu Nirmal: I see, that's why I suggest to put it as *question* on *Stack Overflow*: the very place you can provide necessary details (relevant code, test cases etc.) and have an auditory which can help you. – Dmitry Bychenko Sep 19 '18 at 09:09
  • 1
    You missed parentheses around the whole regex, as is, it matches any string that begins with `7` or any string that contains a `0` any where, see: https://regex101.com/r/osHPJF/1 – Toto Aug 13 '20 at 09:57
  • 1
    @Toto: I see, nice catch! Thank you! – Dmitry Bychenko Aug 13 '20 at 10:01
0
 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)

damith alahakoon
  • 270
  • 4
  • 14
  • You should use character classes instead of these long alternations, for example `[0-25-8]` instead of `(0|1|2|5|6|7|8)` – Toto Aug 13 '20 at 10:00
0

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}$";
Matthew Warman
  • 3,234
  • 2
  • 23
  • 35