Good phones, phones good
Weekly challenge 110 — 26 April 2021
Week 110: 26 Apr 2021
You are given a text file. Write a script to display all valid phone numbers in the given text file.
Valid phone numbers have one of 3 possible formats:
Input: 0044 1148820341 +44 1148820341 44-11-4882-0341 (44) 1148820341 00 1148820341 Output: 0044 1148820341 +44 1148820341 (44) 1148820341
This is just a case of matching against 3 regexes. I allowed leading and trailing spaces, and more than one space where the allowed format has a single space, but output the numbers left justified and with single spaces where allowed.
Having coded this in real life I know that the range of valid UK phone numbers is somewhat larger than stated, and the number of formats encountered in a database where the field accepted any number of any characters is vastly larger.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2021-04-26 use utf8; # Week 110 - task 1 - Valid phone numbers use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; valid_phone_numbers(); sub valid_phone_numbers { my ($phone); # initialise $phone = <DATA>; chomp $phone; say $phone; # check for valid format while ($phone = <DATA>) { say $1 if ($phone =~ m|^\s*(\+\d\d\s+\d+)\s*$| or $phone =~ m|^\s*(\(\d\d\)\s+\d+)\s*$| or $phone =~ m|^\s*(00\d\d\s+\d+)\s*$|); } } __DATA__ 0044 1148820341 +44 1148820341 44-11-4882-0341 (44) 1148820341 00 1148820341
15 lines of code
I completed this challenge after the closing date
and it has not
been submitted to GitHub
Input: 0044 1148820341 +44 1148820341 44-11-4882-0341 (44) 1148820341 00 1148820341 Output: 0044 1148820341 +44 1148820341 (44) 1148820341
Any content of this website which has been created by Peter Campbell Smith is in the public domain