I’m trying to grab all NA phone numbers from a CSV file. The numbers can appear anywhere in each line and each line can also have multiple numbers (separated by commas). The regex I’ve come up with does work, at least it grabs the first phone number in the line. But despite using the “/g” flag it won’t grab any of the other phone numbers. Can anyone suggest what might be wrong with my code?
#!/usr/bin/perl
use warnings;
use diagnostics;
use strict;
my $data_file = "test.csv";
open my $FH, "<", $data_file || die "cannot open file\n";
my @lines = <$FH>;
while (@lines) {
if ((shift @lines) =~ /((\(\d{3}\)\s+|\d{3}-?|\d{3}\.?)(\d{3}-?|\d{3}\.?)\d{4})/g) {
print "$1\n";
} else {
print "No match\n";
}
}
$1is a scalar, and thus cannot contain multiple matches. You might want to try something like this:Or you could try something like this: