Validating phone numbers is a common programming task. Using regular expressions is an efficient approach to solve this problem. Regular expressions provide a flexible method for matching specific character patterns, which is highly useful for phone number validation as formats can vary by country or region.
Basic Components of Regular Expressions
First, let's briefly explore several fundamental components of regular expressions, which are essential for building phone number validation patterns:
- Digit Matching: Using
\dmatches any digit, equivalent to[0-9]. - Quantifiers: Such as
+(one or more),*(zero or more),{n}(exactly n times),{n,}(at least n times), etc., used to specify the number of occurrences of the preceding element. - Optional Elements: Using
?indicates that the preceding element is optional, appearing 0 or 1 times. - Alternation: Using
|indicates alternatives, such as(abc|def)which matches 'abc' or 'def'.
Example: Validating Chinese Mobile Phone Numbers
Assume we need to validate Chinese mobile phone numbers, which are typically 11-digit numbers starting with 13, 14, 15, 17, or 18. A simple regular expression is:
regex^1[3-9]\d{9}$
Explanation:
^indicates the start of a line.1indicates the number starts with the digit 1.[3-9]indicates the second digit can be any number from 3 to 9.\d{9}indicates the following nine digits.$indicates the end of a line.
Example: Validating US Phone Numbers
US phone number formats are typically (xxx) xxx-xxxx or xxx-xxx-xxxx. A corresponding regular expression is:
regex^\(?[2-9]\d{2}\)?[- ]?\d{3}[- ]?\d{4}$
Explanation:
^indicates the start of a line.\(?and\)?indicate that parentheses are optional.[2-9]indicates that the area code does not start with 0 or 1.\d{2}indicates the following two digits.[- ]?indicates that the separator (hyphen or space) between numbers is optional.\d{3}and\d{4}match three and four digits respectively.$indicates the end of a line.
Conclusion
In practical applications, you must tailor the regular expression to the specific phone number format. For example, phone numbers in certain countries may include country codes or special characters, which require adjustments in the regular expression. Testing the validity and accuracy is crucial; tools like Regex101 can be used for validation and testing.