http://qs1969.pair.com?node_id=42281


in reply to How do I parse a telephone number?

I take a slightly different approach because I have to deal with foreign phone numbers also. This sub seems to work pretty good for me. If it can't find a format it just splits it up into groups of threes and sends it back.
HTH
sub fphone { shift; s/[\s|\n|\r]//g; # strip space and newlines s/[-|)|(|.]//g; # strip seperating chars s/(x|ex|ext\.)(.*)$//; # Extension my $ext = $2; # save extension my $n = ''; my $l = length($_); if (($l == 12)||($l == 13)) { m/(\+\d{2}|\d{3})(\d{3})(\d{3})(\d{3})/; $n .= "$1 $2 $3 $4"; } elsif (length == 11) { # us with leading 1 s/[+]//g; m/(\D)?(\d{3})(\d{3})(\d{4})/; $n .= "$1 " if $1 =~ /\d/; $n .= "($2) $3-$4"; } elsif ($l == 10) { # us regular s/[+]//g; m/(\d{3})(\d{3})(\d{4})/; $n .= "($1) $2-$3"; } else { s/(\d\d\d)/$1 /g; $n .= $_; } $n .= " Ext. $ext" if $ext; return $n; }