August 2012
Intermediate to advanced
609 pages
19h 16m
English
You want to determine whether a user entered a North
American phone number, including the local area code, in a common
format. These formats include 1234567890, 123-456-7890, 123.456.7890, 123 456 7890, (123) 456 7890, and all related
combinations. If the phone number is valid, you want to convert it to
your standard format, (123) 456-7890, so that
your phone number records are consistent.
A regular expression can easily check whether a user entered something that looks like a valid phone number. By using capturing groups to remember each set of digits, the same regular expression can be used to replace the subject text with precisely the format you want.
^\(?([0-9]{3})\)?[-.●]?([0-9]{3})[-.●]?([0-9]{4})$| Regex options: None |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
($1)●$2-$3| Replacement text flavors: .NET, Java, JavaScript, Perl, PHP |
(\1)●\2-\3| Replacement text flavors: Python, Ruby |
Regex phoneRegex =
new Regex(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");
if (phoneRegex.IsMatch(subjectString)) {
string formattedPhoneNumber =
phoneRegex.Replace(subjectString, "($1) $2-$3");
} else {
// Invalid phone number
}var phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if (phoneRegex.test(subjectString)) {
var formattedPhoneNumber =
subjectString.replace(phoneRegex, "($1) $2-$3");
} else {
// Invalid phone number
}