C# asp.net for International&;的正则表达式;本地电话号码

C# asp.net for International&;的正则表达式;本地电话号码,c#,asp.net,regex,C#,Asp.net,Regex,我用下面的正则表达式来表示国际电话号码和一些只能使用这种格式的本地电话号码 国际电话号码 +123 456789123 +123456789123 +12 3456789123 +123456789123 本地电话号码格式(手机号码后接固定电话号码) 我正在使用的正则表达式 ^[\+]{0,1}[1-9]{1}[0-9]{7,11}$ 此正则表达式仅适用于国际数字,无论是否添加前缀+,但不允许任何空格字符 我希望它支持上述格式,如示例所示,还应支持所有国际电话号码 我正在asp.net上工

我用下面的正则表达式来表示国际电话号码和一些只能使用这种格式的本地电话号码

国际电话号码

+123 456789123
+123456789123

+12 3456789123
+123456789123
本地电话号码格式(手机号码后接固定电话号码)

我正在使用的正则表达式

^[\+]{0,1}[1-9]{1}[0-9]{7,11}$
此正则表达式仅适用于国际数字,无论是否添加前缀
+
,但不允许任何空格字符

我希望它支持上述格式,如示例所示,还应支持所有国际电话号码

我正在asp.net上工作,以防有人想知道

更新:

最后,我使用了下面的正则表达式,它也处理扩展

^([\+]?[0-9]{1,3}[\s.-][0-9]{1,12})([\s.-]?[0-9]{1,4}?)$
试试这个:

^[\+]?[1-9]{1,3}\s?[0-9]{6,11}$
试试这个:

^[\+]?[1-9]{1,3}\s?[0-9]{6,11}$

嗨,关于你是regex的一些评论

[\+]{0,1} could be \+?  // easier to read, + as optional
[1-9]{1} could be writen as [1-9]
[0-9]{7,11} should be [0-9\s.-]{7,11}  // phone numbers van contain - or .
你完全是正则表达式

^\+?[1-9][0-9\s.-]{7,11}$
电话号码可以写为

  • 070-3233123
  • 070.3233123

第二次尝试

你可以通过两个步骤来解决你的问题:

通过将范围从11增加到20,首次匹配可能的电话号码

^\+?[1-9][0-9\s.-]{7,20}$

下一步是删除非数字,并验证长度是否在8到12之间

string phone = "070.3233123";

string onlyNumbers= new String(phone.ToCharArray().Where(c => Char.IsDigit(c)).ToArray());

if (onlyNumbers.length > 8 &&  onlyNumbers.length < 12)
{
    // we got a winner
}
string phone=“070.3233123”;
字符串onlyNumbers=新字符串(phone.tocharray().Where(c=>Char.IsDigit(c)).ToArray());
如果(onlyNumbers.length>8&&onlyNumbers.length<12)
{
//我们赢了
}

您好,关于您是regex的一些评论

[\+]{0,1} could be \+?  // easier to read, + as optional
[1-9]{1} could be writen as [1-9]
[0-9]{7,11} should be [0-9\s.-]{7,11}  // phone numbers van contain - or .
你完全是正则表达式

^\+?[1-9][0-9\s.-]{7,11}$
电话号码可以写为

  • 070-3233123
  • 070.3233123

第二次尝试

你可以通过两个步骤来解决你的问题:

通过将范围从11增加到20,首次匹配可能的电话号码

^\+?[1-9][0-9\s.-]{7,20}$

下一步是删除非数字,并验证长度是否在8到12之间

string phone = "070.3233123";

string onlyNumbers= new String(phone.ToCharArray().Where(c => Char.IsDigit(c)).ToArray());

if (onlyNumbers.length > 8 &&  onlyNumbers.length < 12)
{
    // we got a winner
}
string phone=“070.3233123”;
字符串onlyNumbers=新字符串(phone.tocharray().Where(c=>Char.IsDigit(c)).ToArray());
如果(onlyNumbers.length>8&&onlyNumbers.length<12)
{
//我们赢了
}

下面的模式将验证以下格式

^\s*\+?[0-9]\d?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$
结果

+1 (281) 388 0388
+65 281 388-0388
+91 281 388 0388
+65 2813880388
+652813880388
+65(281)3880388
+65281388-0388
+1281388-0388

下面的模式将验证以下格式

^\s*\+?[0-9]\d?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$
结果

+1 (281) 388 0388
+65 281 388-0388
+91 281 388 0388
+65 2813880388
+652813880388
+65(281)3880388
+65281388-0388
+1281388-0388

+1为了得到清晰的答复,我试过它对
+123-456789123
无效,
+123 456789123
是的,很好,你想要7到11个数字,在空白之间。或-of()字符巫婆在11个数字之前不算7。。。让我想想,这也和
+1\uuuuuuuuuuuuuuuuuuuuuuuuuu3
匹配吗?这不准确吗?(注意:我不得不用
下划线
替换
空格
)。+1作为干净的回答,我试过它不适用于
+123-456789123
+123 456789123
是的,很好,你想要7到11个数字,并且在空格之间。或-of()字符巫婆在11个数字之前不算7。。。让我想想,这也和
+1\uuuuuuuuuuuuuuuuuuuuuuuuuu3
匹配吗?这不准确吗?(注意:我必须使用
下划线
替换
空格
)@knowledgeseek更新,这是因为
{3}
(固定长度)应该是
{1,3}
(可变长度)。@knowledgeseek更新,这是因为
{3}
(固定长度)应该是
{1,3}
(可变长度)。