Regular expression to match standard 10 digit phone number

0 votes

I want to write a regular expression for a standard US type phone number that supports the following formats:

###-###-#### 
(###) ###-#### 
### ### #### 
###.###.####


where # means any number. So far I came up with the following expressions

^[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}

I am, however, not quite sure if the last one is correct for the dotted check. I also want to know if there is any way I could write a single expression instead of the 4 different ones that cater to the different formats I mentioned. If so, I am not sure how to do that. And also how do I modify the expression/expressions so that I can also include a condition to support the area code as an optional component. Something like

+1 ### ### ####


where +1 is the area code and it is optional.

Feb 22, 2022 in Others by Rahul
• 9,680 points
1,628 views

1 answer to this question.

0 votes

 If you are also interested in matching on unformatted numbers just make the separator character class optional as [\s.-]?

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


Matches the following

123-456-7890 
(123) 456-7890 
123 456 7890 
123.456.7890 
+91 (123) 456-7890

If you do not want a match on non-US numbers, then use:-

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

answered Feb 22, 2022 by Aditya
• 7,680 points

Related Questions In Others

+1 vote
1 answer

How To Create Phone Number Format XXX-XXX-XXXX In Android

 can try: private String formatNumbersAsCode(CharSequence s) { ...READ MORE

answered Jun 14, 2022 in Others by polo
• 1,480 points
8,869 views
0 votes
1 answer

Excel automatically converting 7 digit CAS number to another number (date?)

Looks like you could use: The formula in D2: =SUBSTITUTE(F ...READ MORE

answered Sep 25, 2022 in Others by narikkadan
• 63,720 points
886 views
+1 vote
1 answer

Excel or Google formula to count occurrences of an 8-digit number within a text string

To match an eight-digit number, you may ...READ MORE

answered Dec 24, 2022 in Others by narikkadan
• 63,720 points
909 views
0 votes
1 answer

Removing all white-spaces from a string

You can use the 'str_replace_all()' function from ...READ MORE

answered May 16, 2018 in Data Analytics by Bharani
• 4,660 points
580 views
0 votes
1 answer

What Regex to use to identify a Block Hash?

Blockhashes are always 64 characters in length. You ...READ MORE

answered Aug 28, 2018 in Blockchain by digger
• 26,740 points
1,724 views
0 votes
1 answer

R programming logic

Use gsub to match the substring that we want ...READ MORE

answered Nov 16, 2018 in Data Analytics by Maverick
• 10,840 points
642 views
0 votes
1 answer

.replace() regex in Python

No,  .replace() does not support regex. Regular expressions ...READ MORE

answered Nov 26, 2018 in Python by SDeb
• 13,300 points
801 views
0 votes
1 answer

PHP & MySQL: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

Your query seems to have an ERROR ...READ MORE

answered Feb 16, 2022 in Others by Aditya
• 7,680 points
4,677 views
0 votes
1 answer

How to fix "Headers already sent" error in PHP

The functions that send and modify the ...READ MORE

answered Feb 17, 2022 in Others by Aditya
• 7,680 points
7,068 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP