Explore the essential aspects of validating phone numbers using regular expressions, including pattern matching, capturing groups, and handling common number formats. This quiz challenges your understanding of regex syntax and best practices for accurate phone number validation.
Which regex pattern best matches a standard US phone number such as 555-123-4567, enforcing three digits, a hyphen, three digits, another hyphen, and four digits?
Explanation: The correct regex, ^d{3}-d{3}-d{4}$, specifically requires three digits, followed by a hyphen, three more digits, another hyphen, and finally four digits. The second option only allows two digits at the start, which does not match standard US phone numbers. The third option swaps the middle and last groups, altering the intended order. The fourth option omits hyphens, allowing only digits, which fails to restrict the pattern to the expected format.
What regex structure would allow phone numbers to optionally begin with a plus sign and a one to three digit country code, such as +44-555-678-1234?
Explanation: Option one, (+d{1,3}-)?, uses parentheses and the question mark to make the country code optional, correctly covering the case whether the number starts with a plus and up to three digits or not. Option two makes the plus sign optional but does not group the country code specifically, which may improperly match numbers missing the required hyphen after the country code. The third option mandates a three-digit country code and does not make it optional. The fourth option incorrectly uses {3,} to match three or more digits, which is too broad for country codes.
If you want to match phone numbers where the area code is enclosed in parentheses, such as (123) 456-7890, which part should you use in your regex?
Explanation: The first option includes literal parentheses around exactly three digits for the area code, followed by a space, three digits, a hyphen, and four digits—accurately matching the given format. The second option is missing the opening parenthesis, while the third is missing the closing parenthesis and a space after it. The fourth option does not use any parentheses and does not match the intended format.
Which regex pattern will match phone numbers that use either spaces or dashes as separators, such as 123 456 7890 or 123-456-7890?
Explanation: The first regex uses a capturing group ([- ]) for the separator and then a back-reference (1) to ensure the same separator is used throughout. This pattern matches either all spaces or all dashes between sections. The second and third patterns allow any combination of a dash or space between sections, but do not require consistency. The fourth option matches only a dash as the first separator and omits a separator between the last groups.
How can you use a negative lookahead to prevent phone numbers starting with 000, such as 000-555-1234, from being matched?
Explanation: The negative lookahead (?!000) at the start ensures that a number beginning with 000 is not matched, while standard valid numbers are still accepted. The second option restricts results to those that start with 000, which is the opposite of the requirement. The third option incorrectly makes 000 optional, allowing both valid and invalid numbers. The fourth option lacks any restriction and matches all combinations, including those beginning with 000.