Regexdrill

Level 3: Real-world validation Exercise 22 of 52 Pro

US phone number formats

A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.

The task

The whole line must match. Accept (555) 123-4567, or ten digits split 3, 3 and 4 by the same separator throughout: a hyphen, a dot, or nothing.

What this exercise teaches

\(\d{3}\) \d{3}-\d{4}|\d{3}([-.]?)\d{3}\1\d{4} handles the parentheses form, and the backreference forces the same separator twice.

A hint if you get stuck

For the separated forms, capture the first separator (which may be empty) and reuse it with a backreference.

This exercise is part of Pro

Pro unlocks all 52 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Real-world validation