java 手机号码验证的正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16460204/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:00:31  来源:igfitidea点击:

Regular expression for mobile number vaidation?

javaregex

提问by Ashish Sharma

I have following regular expression for following mobile numbers:

我有以下手机号码的正则表达式:

^(([+]|[0]{2})([\d]{1,3})([\s-]{0,1}))?([\d]{10})$

Valid numbers are:

有效数字是:

+123-9854875847
00123 9854875847
+123 9854875847
9878757845

Above expression will not validate if user enter 9 or 11 digit mobile number but if user enter 9 or 11 digit number with +123or +91respectively then it is getting validate because in this part of expression ([\\d]{1,3})last two digits are optional.

上面的表达式将不进行验证,如果用户输入9或11位数的手机号码,但如果用户输入9或11位数字用+123+91因为在表达这一部分分别则越来越验证([\\d]{1,3})最后两位数字是可选的。

So, any way to make this part ([\\s-]{0,1}))?([\\d]{10})not to get combine with this part ([\\d]{1,3})?

那么,有什么办法可以让这部分([\\s-]{0,1}))?([\\d]{10})不与这部分结合([\\d]{1,3})

采纳答案by Bernhard Barker

The question is somewhat unclear, but I presume you want to split the number and the country code.

问题有点不清楚,但我想您想拆分数字和国家/地区代码。

This is quite easy to do by extracting groups. group(i)is the i-th thing in brackets.

通过提取组很容易做到这一点。group(i)i括号中的第 -th 项。

I also applied these simplifications: [\\d]= \\d, {0,1}= ?, [+]= \\+, [0]{2}= 00.

我还应用了这些简化:[\\d]= \\d{0,1}= ?[+]= \\+[0]{2}= 00

Code:

代码:

String regex = "^((\+|00)(\d{1,3})[\s-]?)?(\d{10})$";
String str = "+123-9854875847";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches())
{
   System.out.println("Country = " + m.group(3));
   System.out.println("Data = " + m.group(4));
}

Output:

输出:

Country = 123
Data = 9854875847

Alternative using non-matching groups (?:):(so you can use group(1)and group(2))

使用非匹配组 ( ?:) 的替代方法:(因此您可以使用group(1)group(2)

String regex = "^(?:(?:\+|00)(\d{1,3})[\s-]?)?(\d{10})$";
String str = "+123-9854875847";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches())
{
   System.out.println("Country = " + m.group(1));
   System.out.println("Data = " + m.group(2));
}

Reference.

参考

Related test.

相关测试

回答by melwil

As long as the extension is always separated from the rest of the phone number, your regex will work fine. If there is no such separation, there is no way to correctly validate a phone number.

只要分机号始终与电话号码的其余部分分开,您的正则表达式就可以正常工作。如果没有这种分离,就无法正确验证电话号码。

Also keep in mind that both extensions and phone numbers can vary in length from country to country, so there is no regex that will solve all cases. If you can produce a list of allowed extensions, you can work that into the regex and get better matches, but for many groups of arbitrary length of digits you will get many wrong matches.

还要记住,分机号和电话号码的长度可能因国家/地区而异,因此没有正则表达式可以解决所有情况。如果您可以生成一个允许的扩展名列表,您可以将其处理到正则表达式中并获得更好的匹配,但是对于许多任意长度的数字组,您将获得许多错误的匹配。

I have simplified your regex a bit, so oyu can see @Dukeling's suggestions in practice. Your regex on top, mine on the bottom.

我已经稍微简化了您的正则表达式,因此 oyu 可以在实践中看到 @Dukeling 的建议。你的正则表达式在上面,我的在下面。

^(([+]|[0]{2})([\d]{1,3})([\s-]{0,1}))?([\d]{10})$
^(  (\+|00)    \d{1,3}    [\s-]?)?      \d{10}  $

回答by Hardip

try {


            String mobile_number="india number +919979045000\n" +
                    "india number 9979045000\n" +
                    "china number +86 591 2123654\n" +
                    "Brazil number +55 79 2012345\n" +
                    "it is test all string get mobile number all country"+
                    "Ezipt +20 10 1234567\n" +
                    "France +33 123456789\n" +
                    "Hong Kong  +852 1234 5456\n" +
                    "Mexico +52 55 12345678"+
                    "thanks";

            Pattern p = Pattern.compile("\(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{5}( ?-?[0-9]{3})? ?(\w{1,10}\s?\d{1,6})?");
            List<String> numbers = new ArrayList<String>();
            //mobile_number= mobile_number.replaceAll("\-", "");
            Matcher m = p.matcher("" + mobile_number);
            while (m.find()) {
                numbers.add(m.group());
            }

            p = Pattern.compile("\(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? ?(\w{1,10}\s?\d{1,6})?");
            m = p.matcher("" + mobile_number);
            while (m.find()) {
                numbers.add(m.group());
            }

            p = Pattern.compile("((?:|\+)([0-9]{5})(?: |\-)(0\d|\([0-9]{5}\)|[1-9]{0,5}))");
            m = p.matcher("" + mobile_number);
            while (m.find()) {
                numbers.add(m.group());
            }

            p = Pattern.compile("[0-9]{10}|\(?\+[0-9]{1,3}\)?-?[0-9]{3,5} ?-?[0-9]{4}?");
            m = p.matcher("" + mobile_number);
            while (m.find()) {
                numbers.add(m.group());
            }

            String numberArray=numbers.toString();
            System.out.print(""+numberArray);

            // final result 
            /* [+919979045000, +86 591 2123654, +33 123456789, +52 55 12345678, +919979045000, +86 591 2123654, +55 79 2012345, +20 10 1234567, +33 123456789, +852 1234 5456, +52 55 12345678, +919979045000, 9979045000] */


        } catch (Exception e) {
            e.printStackTrace();
        }

回答by Pritesh Tayade

Best way to take input in two parts i.e country code and mobile number. In that case you can easily validate it (both country code and mobile number) with regex.

输入两部分的最佳方式,即国家代码和手机号码。在这种情况下,您可以使用正则表达式轻松验证它(国家/地区代码和手机号码)。