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

In reply to Request for Comments: Phone Number Finder by Skeeve

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.