Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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
    [ambrus]: (b) a good presentation system that lets the presenter quickly interactively edit the slides live during a presentation, to combine the advantages of blackboard and overhead slide styles in modern tech
    [Corion]: Heh - in university, I cheated on (a) by doing blackboard presentations using chalk. But those were 2 hour presentations, not quick/essential/ reduced presentations where you want to show something quick
    [ambrus]: (either on just one screen or two screens). this is necessary because
    [ambrus]: overhead slide plus blackboard is inconvenient because the lighting conditions are different and they require separate areas you can't quickly repartition, and typing on keyboard is faster and more convenient than writing on a blackboard
    [Corion]: (b) would be cool. I've thought about this doing Pod editing, and even simply regenerating/live updating the browser makes things much more interactive
    [ambrus]: modern computers have way enough processing power to allow this, at least for geeks who are willing to spend a few weeks to learn a tricky new user interface like vim
    [Corion]: ambrus: Well, for mathematical notation, I find blackboard much more convenient than a computer. But when inserting text or moving text around, the computer wins obviously
    [ambrus]: But either of these is a big problem in practice, so I'd need to spend like thirty years of my life to solve (a) and five more years to solve (b)
    [ambrus]: Corion: yes, CURRENTLY the blackboard is more convenient
    [ambrus]: and it's not like I want to ban blackboards anyway

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (10)
    As of 2017-09-26 10:21 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (293 votes). Check out past polls.