验证英国电话号码(Regex c#)
本文关键字:Regex 英国 电话号码 验证 | 更新日期: 2023-09-27 18:16:41
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;
}
这是我有,但我得到错误说Unrecognized escape sequence
。有人能帮忙吗?需要能够有+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}))?$
+447222555555 | +44 7222 555 555 | (0722) 5555555 #2222
<<p> REGEX演示/strong> 您可以试试英国电话号码的正则表达式:
/^'(?0( *'d')?){9,10}$/
此正则表达式将检查英国号码中是否有10或11位数字,以0开始,其中任何数字之间可能有格式化空格,并可选择使用一组方括号表示区号。
同样在你的正则表达式中,你需要添加@来消除这个错误(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})?$
试试:
^('+44''s?7''d{3}|'(?07''d{3}')?)''s?''d{3}''s?''d{3}$
为了使正则表达式能够识别's
, 'd
等,您需要添加双斜杠''
。如果没有,你会得到一个illegal escape character
错误。
这工作得很好,并允许3和4位扩展。它还确保只输入手机号码。英国手机号码以07开头,无论运营商是谁
^('+44's?7'd{3}|'(?07'd{3}')?)'s?'d{3}'s?'d{3}('s?'#('d{4}|'d{3}))?$
试试这个代码,
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;
}
}