Women in Technology

Hear us Roar



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

Full Threads Oldest First

Showing messages 1 through 3 of 3.

  • more appropriately ...
    2003-08-29 12:31:01  anonymous2 [View]

    Not to take away from the main point of the article; it's an ugly practical problem I've dealt with in the past and I wanted to point out another option. Re-stated in the context of the article, please replace the appropriate line in my last post with:

    if ($phone =~ m/^ # beginning of string
    (\d{3})? # maybe an area code
    (\d{7}) # definitely 7 digits
    $ # end of string
    /x) {

    Krishna Sethuraman
    • more appropriately ...
      2003-11-08 10:42:06  anonymous2 [View]

      how to use regular expression to search

      all digits of the pattern in MYSQL


      X-XX-X
      XX-XX-X
      XXX-XX-X
      XXXX-XX-X
      XXXXX-XX-X


      I tried


      mysql> select * from database where field regexp "(\d{2,5}-\d{2}-d{1});

      seems not working

      regards
      M.Karthikeyan
      pl. mail:

      karthik@email.unc.edu
      • more appropriately ...
        2004-01-12 08:36:19  anonymous2 [View]

        it works with
        mysql> select count(*) from database where field1 regexp "[[:digit:]]{2,6}-[[:digit:]]{2}-[[:digit:]]{1}";