java Java中多种模式的电话号码正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3367843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Phone number regex for multiple patterns in Java
提问by TheJediCowboy
I need help writing a regex for a phone number pattern that allows the following formats for a phone number
我需要帮助为电话号码模式编写正则表达式,该模式允许电话号码采用以下格式
1)###-###-#### or 2)#-###-###-#### or 3)###-#### or 4)########## or 5)#######
I am well aware, that you are able to find regex patterns on the internet, but I havent been able to find one that will pass for all these patterns.
我很清楚,您可以在 Internet 上找到正则表达式模式,但我一直无法找到可以通过所有这些模式的正则表达式。
I am using Java
我正在使用 Java
回答by Jeanne Boyarsky
You could use the | (or) operator and multiple patterns.
你可以使用 | (或) 运算符和多个模式。
For example:
(\d{7})|(\d{10)|...
例如:
(\d{7})|(\d{10)|...
回答by Jose Diaz
try ^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$
尝试 ^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$
Hereis an online regular expression evaluator, you can test your patterns against this regex and/or any other.
这是一个在线正则表达式评估器,您可以针对此正则表达式和/或任何其他正则表达式测试您的模式。
回答by Praveen
public int Phone(String num)
{
try
{
String expression = "^(?=.{7,32}$)(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*((\s?x\s?|ext\s?|extension\s?)\d{1,5}){0,1}$";
CharSequence inputStr = num;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
int x=0,y=0;
char[] value=num.toCharArray();
for(int i=0;i<value.length;i++)
{
if(value[i]=='(')
x++;
if(value[i]==')'&&((value[i+1]>=48&&value[i+1]<=57)||value[i+1]=='-'))
y++;
}
if(matcher.matches()&&x==y)
return 1; //valid number
else
return 0; //invalid number
}
catch(Exception ex){return 0;}
}
}
回答by revathi
This regex will match all local numbers and international numbers.
此正则表达式将匹配所有本地号码和国际号码。
String reg= "([\+(]?(\d){2,}[)]?[- \.]?(\d){2,}[- \.]?(\d){2,}[- \.]?(\d){2,}[- \.]?(\d){2,})|([\+(]?(\d){2,}[)]?[- \.]?(\d){2,}[- \.]?(\d){2,}[- \.]?(\d){2,})|([\+(]?(\d){2,}[)]?[- \.]?(\d){2,}[- \.]?(\d){2,})";
1 ###-###-####
2 #-###-###-####
3 ##########
4 +### ##-###-####
5 ###.###.####
6 ### ### ####
1 ###-###-####
2 #-###-###-####
3 ##########
4 +### ##-### -####
5 ###.###.####
6 ### ### ####
7 (###)### ####
7 (###)### ####
All of the above formats would work.
以上所有格式都可以使用。
回答by g1smd
Don't require the user to enter the number in any particular format.
不要求用户以任何特定格式输入号码。
Let them enter it however they want.
让他们随意输入。
Strip any country code and store it for later use, likewise any extension number data if present.
剥离任何国家/地区代码并将其存储以备后用,同样的任何分机号码数据(如果存在)。
Strip any spaces and punctuation.
去除任何空格和标点符号。
Check that the remaining digits are potentially valid by looking at the leading digits and the number length.
通过查看前导数字和数字长度来检查剩余数字是否可能有效。
Store the number in your database in international format with country code, without spaces or punctuation.
将数字以国际格式存储在您的数据库中,带有国家/地区代码,没有空格或标点符号。
Show the number back to the user in a standardised format (either national format or international format with country code) with added spaces and punctuation for readability.
以标准化格式(国家格式或带有国家代码的国际格式)向用户显示数字,并添加空格和标点符号以提高可读性。
回答by robyaw
Try this regular expression:
试试这个正则表达式:
@"^(((\d-)?\d{3}-)?(\d{3}-\d{4})|\d{7}|\d{10})$"
@"^(((\d-)?\d{3}-)?(\d{3}-\d{4})|\d{7}|\d{10})$"
This covers the five scenarios you've described; Alternatively, if you can also accept the following scenarios:
这涵盖了您所描述的五个场景;或者,如果您还可以接受以下场景:
6)###-#######
or
7)#-###-#######
or
8)#-##########
Then this shorter variant will work also:
那么这个较短的变体也将起作用:
@"^(((\d-?)?\d{3}-?)?(\d{3}-?\d{4}))$"
@"^(((\d-?)?\d{3}-?)?(\d{3}-?\d{4}))$"
回答by NebulaFox
Try this
试试这个
^\d?-?(\d{3})?-?\d{3}-?\d{4}$
^\d?-?(\d{3})?-?\d{3}-?\d{4}$
回答by Chris Isnt
This allows extensions, UK international formats, and checks min length 7 characters
这允许扩展,英国国际格式,并检查最小长度 7 个字符
^(?=.{7,32}$)(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*((\s?x\s?|ext\s?|extension\s?)\d{1,5}){0,1}$

