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
Answer from Patrick Parker on Stack Overflow
🌐
How to do in Java
howtodoinjava.com › home › java regular expressions › regex for north american phone number validation
Regex for North American Phone Number Validation
February 16, 2026 - List phoneNumbers = new ArrayList(); phoneNumbers.add("1234567890"); phoneNumbers.add("123-456-7890"); phoneNumbers.add("123.456.7890"); phoneNumbers.add("123 456 7890"); phoneNumbers.add("(123) 456 7890"); //Invalid phone numbers phoneNumbers.add("12345678"); phoneNumbers.add("12-12-111"); String regex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$"; Pattern pattern = Pattern.compile(regex); for(String email : phoneNumbers) { Matcher matcher = pattern.matcher(email); //System.out.println(email +" : "+ matcher.matches()); //If phone number is correct then format it to (123)-456-7890 if(matcher.matches()) { System.out.println(matcher.replaceFirst("($1) $2-$3")); } } ... The above regex will work in JavaScript as well.
🌐
Baeldung
baeldung.com › home › java › validate phone numbers with java regex
Validate Phone Numbers With Java Regex | Baeldung
January 8, 2024 - Learn how to validate different formats of phone numbers using regular expressions.
Top answer
1 of 5
24

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
2 of 5
8

Considering these facts about phone number format:-

  1. Country Code prefix starts with ‘+’ and has 1 to 3 digits
  2. Last part of the number, also known as subscriber number is 4 digits in all of the numbers
  3. 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/

🌐
Stack Abuse
stackabuse.com › java-regular-expressions-validate-phone-number
Java Regular Expressions - Validate Phone Number
November 23, 2021 - In this short article, we'll take a look at how to validate a phone number in Java, using the regex package and multiple Regular Expressions.
🌐
UI Bakery
uibakery.io › regex-library › phone-number-java
Phone number regex Java
The regular expressions below can be used to validate if a string is a valid phone number format and to extract a phone number from a string. Please note that this validation can not tell if a phone number actually exists. ... A simple regex to validate string against a valid international phone number format without delimiters and with an optional plus sign: ... import java.util.regex.Pattern; import java.util.regex.MatchResult; public class Main { public static void main(String []args) { // Validate phone number boolean isMatch = Pattern.compile("^\\+?[1-9][0-9]{7,14}$") .matcher("+122233344
🌐
Icodejava
blog.icodejava.com › tag › us-phone-validation-regex-us-phone-number-validation-java-regex
us phone validation regex us phone number validation java regex – Java and Android Programming Blog
July 12, 2015 - package com.kushal.tools; /** * @author Kushal Paudyal * Last Modified on 05/11/2011 * Java Regular Expression to Validate US and Canada phone numbers. */ import java.util.regex.Pattern; public class RegexUSAndCandaPhoneNumberValidator{ /** * REGEX IS: ^[+]?[01]?[- .]?(([2-9]d{2})|[2-9]d{2})[- .]?d{3}[- .]?d{4}$ * Escape Sequences are added in the following String for back slash () */ static String phoneValidationUSCandaRegex = "^[+]?[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$"; /** * This method compares the parameter string against the US Phone number * regex and returns true if the pattern is matched - meaning the phone * number is valid.
🌐
Mkyong
mkyong.com › home › java › how to validate phone number in java (regular expression)
How to validate phone number in Java (regular expression) - Mkyong.com
August 30, 2012 - Regular expression pattern in Java always is the best method to validate an user’s phone number. Here i provide a regex pattern to determines if the phone number is in correct format, the pattern force starting with 3 digits follow by a “-” and 7 digits at the end.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › phone-number-validation-using-java-regular-expressions
Phone Number validation using Java Regular Expressions
public class MatchPhoneNumber { public static void main(String args[]) { isPhoneValid("1-999-585-4009"); isPhoneValid("999-585-4009"); isPhoneValid("1-585-4009"); isPhoneValid("585-4009"); isPhoneValid("1.999-585-4009"); isPhoneValid("999 585-4009"); isPhoneValid("1 585 4009"); isPhoneValid("111-Java2s"); } public static boolean isPhoneValid(String phone) { boolean retval = false; String phoneNumberPattern = "(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}"; retval = phone.matches(phoneNumberPattern); String msg = "NO MATCH: pattern:" + phone + "\r\n regex: " + phoneNumberPattern; if (retval) { msg = " MATCH: pattern:" + phone + "\r\n regex: " + phoneNumberPattern; } System.out.println(msg + "\r\n"); return retval; } }
🌐
How to do in Java
howtodoinjava.com › home › java regular expressions › regex to validate international phone numbers
Regex to Validate International Phone Numbers
May 28, 2024 - In this regex tutorial, we will learn to validate international phone numbers based on industry-standard notation specified by ITU-T E.123. The rules and conventions used to print international phone numbers vary significantly around the world, so it’s hard to provide meaningful validation for an international phone number unless …
🌐
Javaprogramto
javaprogramto.com › 2020 › 04 › java-phone-number-validation.html
How To Validate Phone Numbers in Java (Regular Expression + Google libphonenumber) JavaProgramTo.com
July 10, 2020 - A quick guide to how to validate phone numbers in java for different countries such as the USA, IN. Example programs with Regular Expression and Google libphonenumber API.
🌐
How to do in Java
howtodoinjava.com › home › string › format a phone number with regex in java
Format a Phone Number with Regex in Java - String
October 11, 2023 - Given below is a Java program that converts a string to a phone number in (###) ###-#### format. It uses the String.replaceFirst() method for matching and replacing the substring using regex. String input = "1234567890"; String number = input.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3"); //(123) 456-7890 ·
🌐
CodingNConcepts
codingnconcepts.com › java › java-regex-to-validate-phone-number
Java Regex to Validate Phone Number - Coding N Concepts
May 27, 2020 - Before we start defining a regex, let’s look at some of the country phone number formats:- Abkhazia +995 442 123456 Afghanistan +93 30 539-0605 Australia +61 2 1255-3456 China +86 (20) 1255-3456 Germany +49 351 125-3456 Indonesia +62 21 6539-0605 Iran +98 (515) 539-0605 Italy +39 06 5398-0605 New Zealand +64 3 539-0605 Philippines +63 35 539-0605 Singapore +65 6396 0605 Thailand +66 2 123 4567 UK +44 141 222-3344 USA +1 (212) 555-3456 Vietnam +84 35 539-0605
🌐
Qodex
qodex.ai › home › all tools › getting started › phone number regex java validator
Phone Number Regex Java Validator — Test Patterns Online
It supports both international and local number formats, helping ensure correct formatting in user input and databases. Try related Java tools like the Email Regex Java Validator, UUID Regex Java Validator, or Date Regex Java Validator for other validations in Java. Phone numbers can appear ...
Rating: 4.9 ​ - ​ 60 votes
Top answer
1 of 9
6

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

2 of 9
4
// 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;
    }
🌐
GitHub
gist.github.com › sangramanand › 1892516
Validating phone number using Java · GitHub
February 23, 2012 - ... This file contains hidden or ... hidden Unicode characters. Learn more about bidirectional Unicode characters ... The regex code accepts a phone number between 10-25 chars length....
🌐
O'Reilly
oreilly.com › library › view › regular-expressions-cookbook › 9781449327453 › ch04s02.html
4.2. Validate and Format North American Phone Numbers - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - 4.2. Validate and Format North American Phone NumbersProblemYou want to determine whether a user entered a North American phone number, including the local area code, in a... - Selection from Regular Expressions Cookbook, 2nd Edition [Book]
Authors   Jan GoyvaertsSteven Levithan
Published   2012
Pages   609
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-match-phone-numbers-in-a-list-to-regex-pattern-in-java
How to Match Phone Numbers in a List to a Regex Pattern in Java ? - GeeksforGeeks
July 23, 2025 - Nowadays it is widely used in computer science, programming, and text processing for tasks such as string matching, data extraction, and validation. In Java, the "java.util.regex" package set of tools for pattern matching. In this article, we will learn how to match phone numbers in a list ...