Regular expression to determine mobile number?

i has the following regular expression for the following mobile numbers:

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

real numbers:

 +123-9854875847 00123 9854875847 +123 9854875847 9878757845 

the above expression will not be checked if the user enters a 9 or 11-digit mobile phone number, but if the user enters a 9 or 11-digit number with +123 or +91 respectively, then he receives a confirmation because in this part of the expression ([\\d]{1,3}) last two digits are optional.

so is there any way to make this part ([\\s-]{0,1}))?([\\d]{10}) incompatible with this part ([\\d]{1,3}) ?

sorry if the question is not clear enough.

+4
source share
4 answers

The question is somewhat unclear, but I assume that you want to separate the number and country code.

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

I also applied these simplifications: [\\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 inconsistent groups ( ?: : (So โ€‹โ€‹you can use group(1) and 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)); } 

Link

Related test .

+3
source

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

Also keep in mind that both numbers and phone numbers can vary in length from country to country, so there is no regular expression that will allow all cases. If you can create a list of allowed extensions, you can work with it in regular expression and get better matches, but for many groups of arbitrary lengths of digits you will get many incorrect matches.

I simplified your regex a bit, so in practice you can see @Dukeling's suggestions. Your regular expression above, mine below.

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

The best way to enter data in two parts: country code and mobile phone number. In this case, you can easily check it (country code and mobile phone number) with a regular expression.

0
source
 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(); } 
0
source

Source: https://habr.com/ru/post/1479893/


All Articles