在我的android项目中,我需要验证用户拥有entered.The移动号码的移动号码可以被格式化为(456)123-1234。移动号码的第一个字符必须是4或5或6。我尝试了这个正则expression.But --它是failed.This,是我尝试过的正则表达式。
"\\([4-6]{1}[0-9]{2}\\)[0-9]{3}\\-[0-9]{4}$";有人能帮我吗?谢谢!
我通过使用这个正则表达式解决了这个问题:
PHONE_REGEX ="\\([4-6]{1}[0-9]{2}\\) [0-9]{3}\\-[0-9]{4}$";发布于 2014-01-18 11:29:42
String sPhoneNumber = "456-8889999";
Pattern pattern = Pattern.compile("\\([4-6]{1}[0-9]{2}\\) [0-9]{3}\\-[0-9]{4}$");
Matcher matcher = pattern.matcher(sPhoneNumber);
if (matcher.matches()) {
System.out.println("Phone Number Valid");
}
else
{
System.out.println("Phone Number must be in the form XXX-XXXXXXX");
}发布于 2014-01-18 11:33:15
匹配格式的正则表达式:
(456)123-1234 (starting with only 4,5 or 6)将是:
^\([4-6]{1}[0-9]{2}\)[0-9]{3}-[0-9]{4}$示例:
http://regex101.com/r/pZ7aL4
如果您想在结束括号后允许一个可选的空格,即:
(456) 123-1234
您可以稍微修改regex,如下所示:
^\([4-6]{1}[0-9]{2}\)\s?[0-9]{3}-[0-9]{4}$发布于 2014-02-08 01:19:05
您应该知道,在北美,有一些地区代码与美国格式共享,作为北美编号计划的一部分,但不与美国电话运营商共享费率结构。例如波多黎各、多米尼加共和国、加拿大等。
如果您使用这个regex阻止出站呼叫到或从长途电话,您需要一个特定的白名单或黑名单-如果您依赖regex,您可能会有一些昂贵的惊喜。
https://stackoverflow.com/questions/21203397
复制相似问题