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 in Others by Rahul
• 8,980 points
16 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 by Aditya
• 7,280 points

Related Questions In Others

0 votes
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 in Others by polo
• 1,500 points
23 views
+1 vote
0 answers

How to split a number with coma betweeen two colunms

Jul 3, 2019 in Others by anonymous
169 views
0 votes
1 answer

I would like to know the Steps involved to remove an Apache Solr from Windows 10 Computer

Hi@Ganapathy, There are several different possibilities for why ...READ MORE

answered Aug 18, 2020 in Others by MD
• 95,360 points
1,407 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
197 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,720 points
665 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
178 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
256 views
0 votes
1 answer
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 in Others by Aditya
• 7,280 points
128 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP