Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

scan for phone numbers

by Anonymous Monk
on Mar 07, 2002 at 21:58 UTC ( #150153=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have to scan a document of received emails for phone numbers. What pattern match would work?

Replies are listed 'Best First'.
Re: scan for phone numbers
by strat (Canon) on Mar 07, 2002 at 22:08 UTC
    There is a telephonenumber format that looks about like (I hope I remember correctly):

    $telephoneNumber =~ /^\+(\d\d)\-(\d+)(:?\-(\d+))+$;/
    where $1 +\d\d stands for the countrycode, $2 the areacode (=citycode), $3 the number, and maybe $4 for a sharing number (don't know the english word).

    e.g: +49-69-44885599
    or: +49-69-123-12345
    (whereas the + is replaced by a local far distance call numbers, e.g. 00 in Germany, and I think 1 in the USA). But I'm not quite sure if this format is really a standard or not (will try to find the definition again; was something with ISDN-Number in X400 or the like).

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: scan for phone numbers
by seattlejohn (Deacon) on Mar 08, 2002 at 03:03 UTC
    I would start by defining your problem more precisely. Which of the following would you consider a phone number that you want to match?
    (310) 555-1212
    +1 (310) 555-1212
    310-555-1212, extension 1234
    011 (46) 12-345678
    1-800-MATTRES (leave off the last "S" for savings ;-)

    etc. etc. It'll be much easier to devise a regex after you've figured out what it is you're actually looking for.

Re: scan for phone numbers
by tstock (Curate) on Mar 08, 2002 at 04:00 UTC
    To follow up on that, I would come up with the best possible function I could come up with to normalize phone numbers, and start a test script that will test a list of phone numbers. As you feel the need to match weirder formats, edit your function to normalize them, run the test script to see if it still works for all cases and then add it to the application you are doing.

    Also if the normalize_phone_number function is placed in a module you can use the module to build your test script.

    I think I would start by treating any non alphanumeric character as a separator, and substitute one or more separators by my normalized separator. then start from the right of the string, first separator after the 6 character is local number, next group is regional, and ... darn, forgot extensions. You're on your own :)

Re: scan for phone numbers
by gellyfish (Monsignor) on Mar 07, 2002 at 22:01 UTC

    /([\d\s-]+)/g - of course that is crap but that is the best you are going to get IMO


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://150153]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2017-08-23 06:43 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (347 votes). Check out past polls.