Basically, you need to take 3 or 4 different patterns and combine them with "|":
String pattern = "\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}";
\d{10}matches 1234567890(?:\d{3}-){2}\d{4}matches 123-456-7890\(\d{3}\)\d{3}-?\d{4}matches (123)456-7890 or (123)4567890
Videos
Basically, you need to take 3 or 4 different patterns and combine them with "|":
String pattern = "\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}";
\d{10}matches 1234567890(?:\d{3}-){2}\d{4}matches 123-456-7890\(\d{3}\)\d{3}-?\d{4}matches (123)456-7890 or (123)4567890
Considering these facts about phone number format:-
- Country Code prefix starts with ‘+’ and has 1 to 3 digits
- Last part of the number, also known as subscriber number is 4 digits in all of the numbers
- Most of the countries have 10 digits phone number after excluding country code. A general observation is that all countries phone number falls somewhere between 8 to 11 digits after excluding country code.
String allCountryRegex = "^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$";
Let's break the regex and understand,
^start of expression(\\+\\d{1,3}( )?)?is optional match of country code between 1 to 3 digits prefixed with '+' symbol, followed by space or no space.((\\(\\d{1,3}\\))|\\d{1,3}is mandatory group of 1 to 3 digits with or without parenthesis followed by hyphen, space or no space.\\d{3,4}[- .]?is mandatory group of 3 or 4 digits followed by hyphen, space or no space\\d{4}is mandatory group of last 4 digits$end of expression
This regex pattern matches most of the countries phone number format including these:-
String Afghanistan = "+93 30 539-0605";
String Australia = "+61 2 1255-3456";
String China = "+86 (20) 1255-3456";
String Germany = "+49 351 125-3456";
String India = "+91 9876543210";
String Indonesia = "+62 21 6539-0605";
String Iran = "+98 (515) 539-0605";
String Italy = "+39 06 5398-0605";
String NewZealand = "+64 3 539-0605";
String Philippines = "+63 35 539-0605";
String Singapore = "+65 6396 0605";
String Thailand = "+66 2 123 4567";
String UK = "+44 141 222-3344";
String USA = "+1 (212) 555-3456";
String Vietnam = "+84 35 539-0605";
Source:https://codingnconcepts.com/java/java-regex-for-phone-number/
A regex to match your format:
(456)123-1234 (starting with only 4,5 or 6)
Would be:
^\([4-6]{1}[0-9]{2}\)[0-9]{3}-[0-9]{4}$
Example:
http://regex101.com/r/pZ7aL4
If you wanted to allow to an optional space after the closing parenthesis, ie:
(456) 123-1234
You would modify regex slightly like this:
^\([4-6]{1}[0-9]{2}\)\s?[0-9]{3}-[0-9]{4}$
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");
}
You can use simple String.matches(regex) to test any string against a regex pattern instead of using Pattern and Matcher classes.
Sample:
boolean isValid = phoneString.matches(regexPattern);
Find more examples
Here is the regex pattern as per your input string:
\+\d(-\d{3}){2}-\d{4}
Online demo
Better use Spring validation annotation for validation.
Example
// The Regex not validate mobile number, which is in internation format.
// The Following code work for me.
// I have use libphonenumber library to validate Number from below link.
// http://repo1.maven.org/maven2/com/googlecode/libphonenumber/libphonenumber/8.0.1/
// https://github.com/googlei18n/libphonenumber
// Here, is my source code.
public boolean isMobileNumberValid(String phoneNumber)
{
boolean isValid = false;
// Use the libphonenumber library to validate Number
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber swissNumberProto =null ;
try {
swissNumberProto = phoneUtil.parse(phoneNumber, "CH");
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
if(phoneUtil.isValidNumber(swissNumberProto))
{
isValid = true;
}
// The Library failed to validate number if it contains - sign
// thus use regex to validate Mobile Number.
String regex = "[0-9*#+() -]*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
You can match these numbers using the following regex:
String rx = "[\\(]?\\d{3}[\\)]?([-.]?)\\s*\\d{3}\\1\\s*\\d{4}";
See IDEONE demo
The ([-.]?) construct is a capturing group that captures and stores the matched text in some buffer, and then we can access that text using \\1 backreference later.
Note that in case you need a whole string match, use String.matches() method with the regex.
For this, you need to use capturing group and backrefernce.
\\(?\\d{3}\\)?([-.]?[\\s]*)\\d{3}\\1\\d{4}
Add anchors if necessary.
^\\(?\\d{3}\\)?([-.]?[\\s]*)\\d{3}\\1\\d{4}$
DEMO
You could use the | (or) operator and multiple patterns.
For example:
(\d{7})|(\d{10)| ...
try ^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$
Here is an online regular expression evaluator, you can test your patterns against this regex and/or any other.