RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for a US phone number

/^(?:\(([2-9][0-8]\d)\)|([2-9][0-8]\d))[-.\s]?([2-9]\d{2})[-.\s]?(\d{4})$/

US numbering has rules regexes can enforce: an area code never starts with 0 or 1, and its second digit is never 9; an exchange code never starts with 0 or 1. This accepts the four separator styles people actually type and captures the three parts so you can normalise them.

What each part does

\( … \)|…the area code either fully in parentheses or with none at all — never half
[2-9][0-8]\darea code — first digit 2-9, second 0-8, third anything
[-.\s]?an optional single separator: hyphen, dot or space
[2-9]\d{2}exchange code — never starts with 0 or 1
\d{4}the four-digit subscriber number

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
(415) 555-2671matches(415) 555-2671
415-555-2671matches415-555-2671
415.555.2671matches415.555.2671
4155552671matches4155552671
(415 555-2671no match
115-555-2671no match
415-055-2671no match
+1 415 555 2671no match

The mistake to avoid

Anchoring with ^...$ and then feeding it a number that still carries a +1 country code. Strip the country code first, or the whole match fails silently.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers