Women in Technology

Hear us Roar



Subject:   Better US Phone Number Regex
Date:   2003-09-02 11:30:10
From:   anonymous2
I got a couple of emails about how to improve the phone number regex above. Here's a better version based on something from the Perl Cookbook, 2ed.



#!/usr/bin/perl

my @tests = ( #These match
"314-555-4000",
"800-555-4400",
"(314)555-4000",
"314.555.4000",
"1 800-555-4000",
"1-800-555-4000",
#These Fail
"1 800 555 5555",
"(800) 555 5555",
"800 555 5555",
"8005554444",
"1888-555-5555",
"1-800.555.4000",
"1-800-555.4000",
"1 800 555-4444",
"1-800 555-4000",
"800.555-4000",
"555-4000",
"aasdklfjklas",
"1234-123-12345",
"(800-555-1212",
"800)555-1212",
"800)-555-1212",
"800555-1212",
);

foreach my $test (@tests) {
if ( $test =~ m/
^
(?:
(?:
1 (?: \s | ([-.]) ) # 1 followed by optional separator
\d\d\d # followed by area code
( (?(1) \1 | [-.] ) ) # followed by separator
)
| # ... or ...
(?: \(\d\d\d\) \s? ) # area code with parens
| # ... or ...
(?: \d\d\d ([-.]) ) # area code with separator
)
\d\d\d # prefix
(?(2) # match separator from "1 followed by..."
\2 # clause
|
(?(3) \3 | [-.] ) # or match separator from "area code with
) # separator" clause
\d\d\d\d # exchange
$
/x ) {
print "Matched on $test [ $1 : $2 : $3 ]\n";
}
else {
print "Failed match on $test\n";
}
}