Problem 2: Matching phone numbers

Validating phone numbers is another tricky task depending on the type of input that you get. Having phone numbers from out of the state which require an area code, or international numbers which require a prefix will add complexity to the regular expression, as does the individual preferences that people have for entering phone numbers (some put dashes or whitespace while others do not for example).

Below are a few phone numbers that you might encounter when using real data, write a single regular expressions that matches the number and captures the proper area code.

Exercise 2: Matching phone numbers
Task Text Capture Groups  
capture 415-555-1234 415 To be completed
capture 650-555-2345 650 To be completed
capture (416)555-3456 416 To be completed
capture 202 555 4567 202 To be completed
capture 4035555678 403 To be completed
capture 1 416 555 9292 416 To be completed
Solution

To grab the area code from the phone numbers, we can simply capture the first three digits, using the expression (\d{3}).

However, to match the full phone number as well, we can use the expression 1?[\s-]?\(?(\d{3})\)?[\s-]?\d{3}[\s-]?\d{4}. This breaks down into the country code '1?', the captured area code '\(?(\d{3})\)?', and the rest of the digits '\d{3}' and '\d{4}' respectively. We use '[\s-]?' to catch the space or dashes between each component.

Solve the above task to continue on to the next problem, or read the Solution.