Regular expression to match standard 10 digit phone number

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Regular expression to match standard 10 digit phone number

Easy-peasy Phone Number Regular Expression πŸ“ž

Alright, folks! πŸŽ‰ Today, we're diving into the exciting world of regular expressions and tackling a common problem: matching standard 10-digit phone numbers. πŸ’ͺ Whether you're a tech geek or just a curious reader, buckle up and let's conquer this challenge together! πŸ™Œ

The Quest for the Perfect Phone Number Format 🌟

Our goal is to create a regular expression that can match the following standard US phone number formats:

  • ###-###-####

  • (###) ###-####

  • ###

  • ###.###.####

πŸ“± Each format has its own unique style, but they all consist of 10 digits. Luckily, regular expressions are here to save the day! πŸ¦Έβ€β™€οΈπŸ¦Έβ€β™‚οΈ

Here are the regular expressions that I came up with for each format:

  1. ^[1-9]\d{2}-\d{3}-\d{4}
  2. ^\(\d{3}\)\s\d{3}-\d{4}
  3. ^[1-9]\d{2}\s\d{3}\s\d{4}
  4. ^[1-9]\d{2}\.\d{3}\.\d{4}

πŸ€” Do they look intimidating? Fear not! Let's break them down step by step:

  1. The first format ^[1-9]\d{2}-\d{3}-\d{4} matches a three-digit area code, followed by a hyphen, and then two sets of three digits separated by hyphens. For example, 123-456-7890.

  2. The second format ^\(\d{3}\)\s\d{3}-\d{4} matches a three-digit area code enclosed in parentheses, followed by a space, and then three digits, hyphen, and four more digits. For example, (123) 456-7890.

  3. The third format ^[1-9]\d{2}\s\d{3}\s\d{4} matches a three-digit area code, followed by a space, and then three more sets of three digits separated by spaces. For example, 123 456 7890.

  4. The fourth format ^[1-9]\d{2}\.\d{3}\.\d{4} matches a three-digit area code, followed by a dot, and then two more sets of three digits separated by dots. For example, 123.456.7890.

πŸŽ‰ Boom! We've dissected these complex-looking expressions. But wait β€” our friend asked if there's a way to condense it into a single expression. Can we do it? Absolutely! 😎

Uniting the Phone Number Formats 🀝

Now, let's combine these four expressions into a stylish all-in-one package! 🎁

^([1-9]\d{2})[-\.\s]?(\d{3})[-\.\s]?(\d{4})$

Impressive, right? This mighty expression covers all the previous formats and even handles variations in the separators (dash, dot, or space) with the help of the [-\.\s]? part. πŸ™Œ

The Optional Area Code Adventure 🌐

The final challenge awaits! Our friend wanted to make the area code optional. Let's give it a go! πŸ’ͺ

To achieve this, we can slightly modify our all-in-one expression like this:

^(\+1\s)?([1-9]\d{2})[-\.\s]?(\d{3})[-\.\s]?(\d{4})$

In this fiery expression, (\+1\s)? allows for an optional "+1" area code followed by a space. So, now our expression can handle +1 123-456-7890 as well. πŸ’₯

Time to Debut Your Superpowers! ⚑️

Congratulations, my bold and fearless readers! πŸ₯³ You've successfully embarked on a journey to conquer the world of regular expressions and match those elusive 10-digit phone numbers in various formats. Now it's time to unleash your newfound powers!

But before I go, share your thoughts! Have you faced any regex challenges lately? Let's discuss in the comments below. And remember, keep spreading the knowledge and joy of regex! πŸ™

May your code be clean and your phone numbers always standardized! πŸ“žβœ¨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my