RegEx: Uk Landline, Mobile Numbers

I struggled with finding the right solution: -

I need a regex expression that matches all UK phone numbers and mobile phones.

So far this one seems to cover most UK numbers:

^0\d{2,4}[ -]{1}[\d]{3}[\d -]{1}[\d -]{1}[\d]{1,4}$

However, mobile numbers do not work with this regular expression or phone numbers recorded in one solid block, such as 01234567890.

Can someone help me create the required regex expression?

+3
source share
6 answers

Will it be a regex?

//  using System.Text.RegularExpressions;

/// <summary>
///  Regular expression built for C# on: Wed, Sep 8, 2010, 06:38:28 
///  Using Expresso Version: 3.0.2766, http://www.ultrapico.com
///  
///  A description of the regular expression:
///  
///  [1]: A numbered capture group. [\+44], zero or one repetitions
///      \+44
///          Literal +
///          44
///  [2]: A numbered capture group. [\s+], zero or one repetitions
///      Whitespace, one or more repetitions
///  [3]: A numbered capture group. [\(?]
///      Literal (, zero or one repetitions
///  [area_code]: A named capture group. [(\d{1,5}|\d{4}\s+?\d{1,2})]
///      [4]: A numbered capture group. [\d{1,5}|\d{4}\s+?\d{1,2}]
///          Select from 2 alternatives
///              Any digit, between 1 and 5 repetitions
///              \d{4}\s+?\d{1,2}
///                  Any digit, exactly 4 repetitions
///                  Whitespace, one or more repetitions, as few as possible
///                  Any digit, between 1 and 2 repetitions
///  [5]: A numbered capture group. [\)?]
///      Literal ), zero or one repetitions
///  [6]: A numbered capture group. [\s+|-], zero or one repetitions
///      Select from 2 alternatives
///          Whitespace, one or more repetitions
///          -
///  [tel_no]: A named capture group. [(\d{1,4}(\s+|-)?\d{1,4}|(\d{6}))]
///      [7]: A numbered capture group. [\d{1,4}(\s+|-)?\d{1,4}|(\d{6})]
///          Select from 2 alternatives
///              \d{1,4}(\s+|-)?\d{1,4}
///                  Any digit, between 1 and 4 repetitions
///                  [8]: A numbered capture group. [\s+|-], zero or one repetitions
///                      Select from 2 alternatives
///                          Whitespace, one or more repetitions
///                          -
///                  Any digit, between 1 and 4 repetitions
///              [9]: A numbered capture group. [\d{6}]
///                  Any digit, exactly 6 repetitions
///  
///
/// </summary>
public Regex MyRegex = new Regex(
      "(\\+44)?\r\n(\\s+)?\r\n(\\(?)\r\n(?<area_code>(\\d{1,5}|\\d{4}\\s+"+
      "?\\d{1,2}))(\\)?)\r\n(\\s+|-)?\r\n(?<tel_no>\r\n(\\d{1,4}\r\n(\\s+|-"+
      ")?\\d{1,4}\r\n|(\\d{6})\r\n))",
    RegexOptions.IgnoreCase
    | RegexOptions.Singleline
    | RegexOptions.ExplicitCapture
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );



//// Replace the matched text in the InputText using the replacement pattern
// string result = MyRegex.Replace(InputText,MyRegexReplace);

//// Split the InputText wherever the regex matches
// string[] results = MyRegex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = MyRegex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = MyRegex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = MyRegex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = MyRegex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = MyRegex.GetGroupNumbers();

, . , area_code tel_no, .

+2
  [\d -]{1}

: .

  01000 123456

01000 . 123456 .

, .

  ^\s*(?(020[7,8]{1})?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3})?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*|[0-9]+[ ]?[0-9]+$

- .

[7,8] 7 8. .

3 7 8.

020 - 2 + 8; . 023, 024, 028 029.

[1-9] {1} [1-9]

[]? \s?

0, ?

^ (0.... | 0.... | 0.... | 0....) $ ^ 0 (.... |.... |.... |....) $

. ([1] | [2] | [3] | [7]) {1} [1237].

: 2 + 8, 3 + 7, 3 + 6, 4 + 6, 4 + 5, 5 + 5, 5 + 4. , , . ; DIGITS.

1: ,

, . , +44, 011 44, 00 44 ; 0. , : (020) 3555 7788 00 (44) 203 555 7788 02035-557-788, . . , . .

  ^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{5}\)?[\s-]?\d{4,5}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$

, 00 011 , , . , + . 44 , , 0 , , ( ). , 0 ( ).

NDC ( ) 2 + 8, 3 + 7, 3 + 6, 4 + 6, 4 + 5, 5 + 5 5 + 4 / . / , , , . GB. , .

, , x, ext , #, . , .

, 01 07 - . , . .

2: NSN,

GB , NSN, , .

  ^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)(44)\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d-]+)(?:((?:x|ext\.?\s?|\#)\d+)?)$

, "44" $1, , , , $1 .

$3 .

NSN ( , ) $2.

3. NSN

, $2 RegEx .

, .

, ,

  ^7([45789]\d{2}|624)\d{6}$

-

  ^9[018]\d{8}$

: , -, , VoIP ..

, , NSN - .

4:

NSN , , . 442035557788.

5:

+44 0.

, 03,

  ^44(3\d{2})(\d{3])(\d{4})$

  0$1 $2 $3 or as +44 $1 $2 $3

, 02,

  ^44(2\d)(\d{4})(\d{4})$ 

  (0$1) $2 $3 or as +44 $1 $2 $3

. , . : http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers

+4

, , - , :

^ 0 (\ d?) {10} $

:

  • 01234567890
  • 01234 234567
  • 0121 3423 456
  • 01213 423456
  • 01000 123456

:

  • 01 2 3 4 5 6 7 8 9 0

, , , , .

+3

, . , , , .. :

@"^(([0]{1})|([\+][4]{2}))([1]|[2]|[3]|[7]){1}\d{8,9}$"

0 +44 ( ) - , 0044, .
1, 2, 3 7.
8 9 .

, : http://en.wikipedia.org/wiki/Telephone_numbers_in_the_United_Kingdom

+2

, Braemoor Software, .

JS, , . ( , , ), .

0

, a + .

(Javascript)

var tel=document.getElementById("tel").value;
tel.substr(0,1).replace(/[^+0-9]/g,'')+tel.substr(1).replace(/[^0-9]/g,'')

, +, 7 15 ( ), +44 (). , +44, +440 0, 2 7, 1, , 0, , . (, 0203 , 0703 , 0103 ). , 025 ( 0205), .

/(^\+(?!44)[0-9]{7,15}$)|(^(\+440?|0)(([27][0-9]{9}$)|(1[1-9][0-9]{7,8}$)))/

Its main purpose is to determine the correct starting digit for an unincorporated number, followed by the correct number of digits. It does not display if the local subscriber number is 5, 6, 7 or 8 digits. He does not apply a ban on the initial "1" or "0" in the subscriber number, about which I can not find any information on whether these old rules are respected. UK phone rules do not apply to properly formatted international phone numbers from outside the UK.

0
source

Source: https://habr.com/ru/post/1763836/


All Articles