#!/usr/bin/perl -w # # phone number formatter use strict; my @numbers = ('0208 477 1376', '080 080 080 0', '0845 07 98 666', '0796 87 28 601', '01787 278545'); # need 01x1 and 011x test cases, and more 08 test cases foreach my $phone (@numbers) { $phone =~ s/ //g; # remove the idiotic pre-formatting my ($code, $local_code, $local_number); if ($phone =~ m/(02\d)(\d+)/) { # london/cardiff/new location numbers: 3 4 4, 3 3 4 # note that I count 0 as part of the code here $code = $1; ($local_code, $local_number) = nice_split($2); } elsif ($phone =~ m/(0[38]\d\d|01\d1|011\d)(\d+)/) { # newish location numbers with three digit codes starting 1, and # 08/03 non geographic numbers: 4 3 3, 4 3 4, 4 4 4 $code = $1; ($local_code, $local_number) = nice_split($2); } else { $phone =~ m/(0\d{4})(\d+)/; # everything else, like mobile numbers # 5 3 3 or 5 3 4 $code = $1; ($local_code, $local_number) = nice_split($2); } print "$phone -> ($code) $local_code $local_number\n"; } sub nice_split { my $number = shift; my $split_at_char = int(length($number)/2); $number =~ m/(\d{$split_at_char})(\d+)/; return ($1, $2); }