ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Five Habits for Successful Regular Expressions
Subject:   US phone numbers
Date:   2003-08-29 12:20:26
From:   anonymous2
Matching phone numbers can be tricky (it's a great exercise for 'fuzzy logic' via regexps in Perl). I've cut that gordian knot in the past by sidestepping the formatting issue:


# remove anything nonnumeric
$origphone = $phone;
$phone =~ s/\D//g;


# beginning of string, (optional 3 digits) followed by (mandatory 7 digits), end of string
if ($phone =~ /^(\d{3})?(\d{7})$/) {
($areacode, $phonenumber) = ($1,$2);
# XXX additional tests here to make sure areacode, phone don't start
# with 0 or 1, or stick it into the regexp
} else {
warn "Number ($origphone) doesn't look like a valid US phone number\n";
}


Krishna Sethuraman


1 to 1 of 1
  1. more appropriately ...
    2003-08-29 12:31:01  anonymous2 [View]

1 to 1 of 1