#!/usr/bin/perl
my $us_phone_regex ='1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?';
my @tests =("1-234-567-8901","1-234-567-8901 x1234","1-234-567-8901 ext1234","1 (234) 567-8901","1.234.567.8901","1/234/567/8901","12345678901","not a phone number");
foreach my $num (@tests){if( $num =~ m/$us_phone_regex/ ){print "match [$1-$2-$3]\n" if not defined $4;print "match [$1-$2-$3 $5]\n" if defined $4;}else{print "no match [$num]\n";}}
## Extract all phone numbers from an arbitrary file.#my $external_filename ='http://web.textfiles.com/ezines/PHREAKSANDGEEKS/PnG-spring05.txt';my @external_file = `curl $external_filename`;foreach my $line (@external_file){if( $line =~ m/$us_phone_regex/ ){print "match $1 $2 $3\n";}}
Validation Results
Result from isPossibleNumber() trueResult from isValidNumber() true
Formatting Results:
E164 format +12345678901Original format (234) 567-8901 ext. 123National format (234) 567-8901 ext. 123International format +1 234-567-8901 ext. 123Out-of-country format from US 1 (234) 567-8901 ext. 123Out-of-country format from CH 00 1 234-567-8901 ext. 123
Validation Results
Result from isPossibleNumber() trueResult from isValidNumber() true
Formatting Results
E164 format +61299999999Original format 61 2 9999 9999National format (02) 9999 9999International format +61 2 9999 9999Out-of-country format from US 011 61 2 9999 9999Out-of-country format from CH 00 61 2 9999 9999