HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Regular Expressions / Java Regex for North American Phone Numbers

Java Regex for North American Phone Numbers

In this regex tutorial, we will learn to validate user entered phone numbers for a specific format (in this example numbers are formatted in north American format) and if numbers are correct then reformat them to a standard format for display. I have tested formats including 1234567890, 123-456-7890, 123.456.7890, 123 456 7890, (123) 456 7890, and all such combinations.

Using Regex to Validate North American Phone Numbers

Regex : ^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$

Above regular expression can be used to validate all formats of phone numbers to check if they are valid north American phone numbers.

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

Output:

1234567890 : 		true
123-456-7890 : 		true
123.456.7890 : 		true
123 456 7890 : 		true
(123) 456 7890 : 	true

12345678 : 			false
12-12-111 : 		false

Using Regex to format North American Phone Numbers

Regex : ($1) $2-$3

Use above regex to reformat phone numbers validated in above step to reformat in a consistent way to store/display purpose.

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"));
	}
}

Output:

(123) 456-7890
(123) 456-7890
(123) 456-7890
(123) 456-7890
(123) 456-7890

Above regex will work in java-script as well. So keep these regex handy when you need them next time.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. sonnie

    July 3, 2018

    How do you ensure that all the valid phone numbers start with the digits “123”

  2. Conor

    November 12, 2014

    Hi great website,

    Do you know how to make a spellchecker with Soundex algorithm?

    Thanks

Comments are closed on this article!

Search Tutorials

Java Regex Tutorial

  • Java Regex – Introduction
  • Java Regex : Alphanumeric
  • Java Regex : Credit Card Numbers
  • Java Regex : Canadian Postcodes
  • Java Regex : Currency Symbol
  • Java Regex : Date Format
  • Java Regex : Email Address
  • Java Regex : Password
  • Java Regex : Greek Characters
  • Java Regex : ISBNs
  • Java Regex : Min/Max Length
  • Java Regex : No. of Lines
  • Java Regex : No. of Words
  • Java Regex : SSN
  • Java Regex : UK Postal Codes
  • Java Regex : US Postal Codes
  • Java Regex : Trademark Symbol
  • Java Regex : Intl Phone Numbers
  • North American Phone Numbers

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces