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-9]\d{2}-\d{3}-\d{4}
^\(\d{3}\)\s\d{3}-\d{4}
^[1-9]\d{2}\s\d{3}\s\d{4}
^[1-9]\d{2}\.\d{3}\.\d{4}
π€ Do they look intimidating? Fear not! Let's break them down step by step:
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.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.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.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.
