US phone numbers

US phone numbers#

Typical phone numbers in the US are of the form 212-555-1212, i.e., a three-digit area code, a hyphen, a three-digit segment, a hyphen, and a four-digit segment.

Write a regular expression for matching these numbers; use repetition, and make sure to use capturing groups for all three numerical parts.

Test your regular expression carefully—we won’t be ‘marking’ it. Convince yourself you got it right!

(NB that it is probably a bad idea to try to intensively validate a phone number. People often have extensions, and it’s possible for numbers to be shorter or longer than the standard form.)