RegEx:英国座机,手机号码 [英] RegEx: Uk Landlines, Mobile phone numbers

查看:63
本文介绍了RegEx:英国座机,手机号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找合适的解决方案:-

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}$

但是手机号码不适用于此正则表达式或以单个实体块(例如01234567890)编写的电话号码.

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?

推荐答案

此正则表达式可以吗?

//  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_codetel_no,以分解并更易于提取.

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.

这篇关于RegEx:英国座机,手机号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆