tags:

views:

36

answers:

2

I've been struggling with finding a suitable solution :-

I need an regex expression that will match all UK phone numbers and mobile phones.

So far this one appears to cover most of the UK numbers:

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

However mobile numbers do not work with this regex expression or phone-numbers written in a single solid block such as 01234567890.

Could anyone help me create the required regex expression?

+1  A: 

Strip all whitespace and non-numeric characters and then do the test. It'll be musch , much easier than trying to account for all the possible options around brackets, spaces, etc. Try the following:

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

Starts with 0 or +44 (for international) - I;m sure you could add 0044 if you wanted.
It then has a 1, 2, 3 or 7.
It then has either 8 or 9 digits.

If you want to be even smarter, the following may be a useful reference: http://en.wikipedia.org/wiki/Telephone_numbers_in_the_United_Kingdom

Matt Lacey
+1, but can't be serious about `[0]{1}` and `[4]{2}`… oO Also, there is too much grouping going on.
Tomalak
Thanks for the response, unfortunately i cannot edit the c# to strip the whitespace out of the string.
Eval_Penguin
A: 

Would this regex do?

//  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();

Notice how the spaces and dashes are optional and can be part of it.. also it is now divided into two capture groups called area_code and tel_no to break it down and easier to extract.

tommieb75
i tried that one with no avail :( a standard 01000 123456 failed. so far the closest i've got to perfect is: ^\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]+$ but its rather chunky
Eval_Penguin
I have based that regex based on the wikipedia by Matt's linky..
tommieb75