in reply to Regular Expression Help
Here's the code I tested ...
use strict; my @p = ( '(555) 324-1233', '555 - 324-1234', '555 - 324-1234x204', '1 555 342 1234', ); my $re = qr/(?:1\s*)?\(?(\d{3})\)?\s*-?\s*(\d{3})[\s-]?(\d{4})(?:x(\d+ +))?/; for my $p (@p) { if ($p =~ $re) { printf "phone number = %s\n" . "area code = %s\n" . "exchange = %s\n" . "number = %s\n" . "extention = %s\n", $p, $1, $2, $3, $4 ? $4 : 'none'; } else { print "[$p] doesn't look like a phone number to me\n"; } }
Output:
phone number = (555) 324-1233 area code = 555 exchange = 324 number = 1233 extention = none phone number = 555 - 324-1234 area code = 555 exchange = 324 number = 1234 extention = none phone number = 555 - 324-1234x204 area code = 555 exchange = 324 number = 1234 extention = 204 phone number = 1 555 342 1234 area code = 555 exchange = 342 number = 1234 extention = none
|
|---|