Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

Yes!, I found 159645 and while I find it very good, it is, I guess, too much for my purpose.

I just want to let (in the end) my computer dial phone numbers from my addressbook and maybe from some text.

For this I need to identify potential phone numbers in some text. Here implemented as the DATA section.

I crafted a lengthy regular expression that satisfies my needs and, I hope, most needs of other people. I'd like to make it a bit more flexibel and configurable. Especially in identifying and changing the phone numbers.

The idea is to change the phone numbers found to the shortest string of digits I have to dieal to reach that phone. So no need to verify that a number is correct but, assuming that it is correct, show what I would have to type in.

The rules here in Germany are:

  1. International calls start with "00"
  2. Calls to other areas start with "0"
  3. Calls in my area start with any digit but "0"

I think I allowed most of internationally used notations, but I'm not sure whether or not I interpret them correctly. E.g.: What about the x1234 x-tensions in 001-555-6666 x1234? What do they effectivly mean? What will I have to type in?

But now: Here is my code, open for discussion...

use strict; use warnings; my @numbers; while (<DATA>) { push @numbers, $1 while m#(\+?\d+(?:\s*[/\-()]?\s*\d)*(?:\s*x\s*\d ++)?)#g; # \+? -> optional + # \d+ -> at least one digit # (?: -> followed by # \s* -> optional whitespace # [/\-()]? -> followed by a seperator: / - ( ) # \s* -> optional whitespace # \d -> one digit # )* -> this is optional and might appeat more often # (?: -> followed by maybe an x-xension # \s* -> optional whitespace # x -> 'x' # \s* -> optional whitespace # \d+ -> digits # )? -> that's it... } exit unless @numbers; # Now we got some numbers # try to change them to what I would dial my $minlen = 3; my %replace= ( '+' => '00', # How to dial to other countries '0049' => '0', # What would be my country code and what Do I use i +nstead? '0241' => '', # What is my local area code and what Do I use inste +ad ); my $replace= join '|',map quotemeta,keys %replace; $replace= qr/^($replace)/; # parse each number foreach (@numbers) { s/(?<=\d)\s*\(0\)//; # change the naughty (e.g.) +49(0) to +49 tr/+0-9//cd; # Remove everything but "+" and digits 1 while s/$replace/$replace{$1}/; # make the replacements as often + as possible } # print the resulting numbers line by line print join qq(\n),grep { length($_) >= $minlen; # if they are long enough } @numbers; __DATA__ my area international a) +49241234567-1 my area international b) 0049241234567-2 another area +49211234567-3 same area but without international 0211234567-4 my area 234567-5 international number 001555234567x6

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re: Request for Comments: Phone Number Finder
by Anonymous Monk on Aug 30, 2007 at 08:52 UTC
    can always learn from "too much"
    http://search.cpan.org/dist/phone/

    E.g.: What about the x1234 x-tensions in 001-555-6666 x1234? What do they effectivly mean? What will I have to type in?
    1 2 3 4, duh :)

      1 2 3 4, duh :)

      Sure? I thought so to but read somewhere that 1 2 3 4 is to be pressed after waiting for the connection. So yes, I have to type that, but not at once. But: Is my information correct? Or does it depend (on whatever)?


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        It depends on whatever, sometimes you have to tell the operator "extension 1234 please"