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.
Task | Text | Capture Groups | |
capture | 415-555-1234 | 415 | |
capture | 650-555-2345 | 650 | |
capture | (416)555-3456 | 416 | |
capture | 202 555 4567 | 202 | |
capture | 4035555678 | 403 | |
capture | 1 416 555 9292 | 416 |
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. |