0

This is my first attempt using regex at all, and my first attempt at using perl at all, so its been a struggle. I'm writing a loop to look through an imaged USB and pull out all the phone numbers inside with this

my $phonefilename = 'phoneoutput.txt';
open(FFF, "<usb256.001");
open(FH, '>', $phonefilename) or die $!;
my $phonenumber;
while(<FFF>)
{
    if (/^\+[[:space:]]*[0-9][0-9.[:space:]-]*(\([0-9.[:space:]-]*[0-9][0-9.[:space:]-]*\))?([0-9.[:space:]-]*[0-9][0-9.[:space:]-]*)?([[:space:]]+ext.[0-9.[:space:]-]*[0-9][0-9.[:space:]-]*)?/i)
    {
        $phonenumber = $1;
        print FH "$phonenumber\n";
    }
}
close(FFF);

and I keep getting Use of uninitialized value $phonenumber in concatenation (.) or string at datafinder.pl line 29, any help?

dcalvert
  • 73
  • 1
  • 11
  • Not related to your problem, but it is considered best practice to use the pattern `open my $fh, '<', name` rather than using a bare filehandle. – William Pursell Feb 23 '20 at 21:15
  • Would you be so kind to make diligence to make a research? You rise question which was answered many times. [Stackoverflow 60366817](https://stackoverflow.com/questions/36929485/perl-phone-number-regex), [Stackoverflow 54506391](https://stackoverflow.com/questions/54506391/perl-comprehensive-phone-number-regex),[Stackoverflow 54862088](https://stackoverflow.com/questions/54862088/validating-phone-number-using-perl),[Stackoverflow 28213255](https://stackoverflow.com/questions/28213255/perl-regular-expression-for-phone-numbers),[regexr.com](https://regexr.com/3c53v), – Polar Bear Feb 23 '20 at 23:19
  • There is no universal solution - phone numbers can be written in many forms which can not be _covered_ with simple regex. Only some particular cases can be implemented in code, if you have some _standard_ form of representation of phone number then at least you should provide a few samples, otherwise you refer to something widely abstract. – Polar Bear Feb 23 '20 at 23:22
  • Without phone number samples we do not know how many digits is in phone number, does it include and hyphens, (), spaces, pause symbols,how numbers are grouped. Do you want to validate a form of input or not, or you just want to know that number of digits match expected predefined count? Do you want check for validity of country code, area code, groups matching particular city, part of the city? – Polar Bear Feb 23 '20 at 23:28
  • Visit the following [website](https://regex101.com/) to play around with your _regex pattern_ to achieve desired result. – Polar Bear Feb 23 '20 at 23:30
  • Your regex matches but does not capture anything so $1 is empty while your condition is true. – clamp Feb 23 '20 at 23:50
  • If you really interested in this subject then let me refer you to [Asterisk](https://www.asterisk.org/) [Dial plan](https://wiki.asterisk.org/wiki/display/AST/Dialplan) - [Dialplan example](https://www.voip-info.org/asterisk-dial-plan-working-example/). – Polar Bear Feb 24 '20 at 01:25

1 Answers1

0

Just for fun of it lets assume that we deal with North American number which consists of 11 digits

  • pos1: 1 - North America
  • pos2: 2-4 - Area code
  • pos3: 5-7 - Group1
  • pos4: 8-11 - Group2

The code snippet: validation is implemented on verification that number consists of 11 digits only

use strict;
use warnings;
use feature 'say';

my %number;                                         # has to store number

while(my $data = <DATA>) {                          # walk through data
    chomp $data;                                    # snip eol
    $data =~ tr/0-9//cd;                            # remove spaces, brakets, dashes
    if( length($data) != 11 ) {                     # is number consist of 11 digits?
        say '-' x 40 . "\n"                         #   no  = invalid
          . "Number:  $data is invalid";
    } else {                                        #   yes = valid
        $data =~ /(\d)(\d{3})(\d{3})(\d{4})/;       #   regex and capture
        @number{qw/country area group1 group2/} = ($1,$2,$3,$4);    # store
        say '-' x 40 . "\n"
          . "Belongs to "
          . ($number{country} == 1 ? "North America" : "None North America");
        say "Number:  $data\n"                      #   output
          . "Record:  $number{country} ($number{area}) $number{group1}-$number{group2}\n"
          . "Country: $number{country}\n"
          . "Area:    $number{area}\n"
          . "Group1:  $number{group1}\n"
          . "Group2:  $number{group2}";
    }
}

__DATA__
1 (309) 123-4567
1 312-123-4567
1 (815) 123-456
3 (421) 123-4567
3 (426) 123-4567
17731234567
1872123456

Output

----------------------------------------
Belongs to North America
Number:  13091234567
Record:  1 (309) 123-4567
Country: 1
Area:    309
Group1:  123
Group2:  4567
----------------------------------------
Belongs to North America
Number:  13121234567
Record:  1 (312) 123-4567
Country: 1
Area:    312
Group1:  123
Group2:  4567
----------------------------------------
Number:  1815123456 is invalid
----------------------------------------
Belongs to None North America
Number:  34211234567
Record:  3 (421) 123-4567
Country: 3
Area:    421
Group1:  123
Group2:  4567
----------------------------------------
Belongs to None North America
Number:  34261234567
Record:  3 (426) 123-4567
Country: 3
Area:    426
Group1:  123
Group2:  4567
----------------------------------------
Belongs to North America
Number:  17731234567
Record:  1 (773) 123-4567
Country: 1
Area:    773
Group1:  123
Group2:  4567
----------------------------------------
Number:  1872123456 is invalid
Polar Bear
  • 6,762
  • 1
  • 5
  • 12