Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

How do I parse a telephone number?

by mofo (Acolyte)
on Jul 11, 2000 at 18:45 UTC ( #21984=categorized question: print w/replies, xml ) Need Help??
Contributed by mofo on Jul 11, 2000 at 18:45 UTC
Q&A  > data formatting


Users can enter free-form phone numbers, such as

212-555-1212 (212)555-1212 1-(212)-555-1212
How can extract just the essential numeric parts so that I can put the numbers into a uniform format?

Answer: How do I parse a telephone number?
contributed by Anonymous Monk

Here's an answer that allows the use of spaces instead of dashes, or no spacing at all.

( $areacode, $exchange, $line ) = $phn =~ m/(1[-| ]?)?\(?(\d{3})\)?[-| ]?(\d{3})[-| ]?(\d{4})/;
Works on strings such as the following:
212-555-1212 (212)555-1213 1-(212)-555-1214 1-212-555-1215 212 555 1216 (212) 555 1217 1 (212) 555 1218 1 212 555 1219 12125551210

Answer: How do I parse a telephone number?
contributed by mojotoad

Folks interested in this question might find my thorough (but empirical) treatment of this topic interesting:
Beast of the Number: Parsing the Feral Phone

Answer: How do I parse a telephone number?
contributed by jlistf

( undef, $areacode, $exchange, $line ) = $phn =~ m|(1-)?\(?(\d{3})\)?-?(\d{3})-(\d{4})|;
You might want to do a little more error-checking on the number, but that's the idea.

Answer: How do I parse a telephone number?
contributed by chromatic

Here's a regex-less solution:

sub parse_phonenumber { local $_ = shift; tr/()\- //d; # eliminate punctuation my( $are, $three, $four ); my $line = substr $_, -4, 4, ''; my $exch = substr $_, -3, 3, ''; my $area = substr $_, -3, 3, ''; ( $area, $exch, $line ) }
Answer: How do I parse a telephone number?
contributed by jdporter

The only CPAN module that comes close to addressing the problem in a comprehensive way is the Number::Phone family of classes. They do a lot more than mere parsing; you may be particularly interested in their format method.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (5)
    As of 2020-09-21 10:39 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (125 votes). Check out past polls.