简体   繁体   English

验证英国电话号码(Regex C#)

[英]Validating UK phone number (Regex C#)

public static bool ValidatePhoneNumber(string number)
{
    return Regex.Match(number, "^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$", RegexOptions.IgnoreCase).Success;
}

This is what I have but I get errors saying Unrecognized escape sequence . 这就是我所拥有的,但我得到错误说Unrecognized escape sequence Can anbody help? 有人可以帮忙吗? needs to be able to have +44 . 需要能够拥有+44

You may try this regex if you are trying to get it with +44 如果你试图用+44获得它,你可以试试这个正则表达式

^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$

This will match for 这将匹配

+447222555555 | +44 7222 555 555 | (0722) 5555555 #2222

REGEX DEMO REGEX DEMO


You can try this regex for UK phone numbers: 你可以试试这个正则表达式的英国电话号码:

/^\(?0( *\d\)?){9,10}$/

This regex will check for 10 or 11 digit numbers which are there in UK numbers, starting with a 0, which may have formatting spaces between any of the digits, and optionally a set of brackets for the area code. 此正则表达式将检查英国数字中的10或11位数字,从0开始,可以在任何数字之间设置格式空格,也可以选择区域代码的一组括号。

Also in your regex you need to add @ to get rid of that error( Unrecognized escape sequence ): 同样在你的正则表达式中你需要添加@来摆脱那个错误( Unrecognized escape sequence ):

public static bool ValidatePhoneNumber(string number)
{
   return Regex.Match(number, @"^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$", RegexOptions.IgnoreCase).Success;
}

这是一个非常可靠的正则表达式,将处理区号,分机号码和+44国际代码以及手机号码甚至10位数字:

^(?:(?:\(?(?: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}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$

Try using this: 试试这个:

^(\+44\\s?7\\d{3}|\(?07\\d{3}\)?)\\s?\\d{3}\\s?\\d{3}$

In order for the regex to recognize the \\s , \\d , etc you need to put double slash \\\\ . 为了让正则表达式识别\\s\\d等,你需要加上双斜杠\\\\ If not you'll get an illegal escape character error. 如果不是,您将收到illegal escape character错误。

This works well and allows 3 and 4 digit extensions. 这很好用,允许3和4位数的扩展。 It also ensures that only mobile numbers are entered. 它还确保只输入手机号码。 UK mobile numbers start with 07 regardless of the provider 无论提供商是谁,英国手机号码都以07开头

^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}(\s?\#(\d{4}|\d{3}))?$

Try this code, 试试这段代码,

    using System.Text.RegularExpressions; 
    public static bool CheckNumber(string strPhoneNumber)
    {
            string MatchNumberPattern = "^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$";
            if (strPhoneNumber != null)
            {
                return Regex.IsMatch(strPhoneNumber, MatchNumberPattern);
            }
            else
            {
                return false;
            }
     }

Hope, this code helps you. 希望,这段代码可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM