简体   繁体   中英

Perl regex doesn't grab multiple North American phone numbers in a line

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";
}
}

$1 is a scalar, and thus cannot contain multiple matches. You might want to try something like this:

my @matches = ((shift @lines) =~ /((?:\(\d{3}\)\s+|\d{3}-?|\d{3}\.?)(?:\d{3}-?|\d{3}\.?)\d{4})/g);
if (@matches) {
    print join("\n", @matches)."\n";
} else {
    print "No match\n";
}

Or you could try something like this:

my $line = shift @lines;
if ($line =~ /((\(\d{3}\)\s+|\d{3}-?|\d{3}\.?)(\d{3}-?|\d{3}\.?)\d{4})/) {
    while ($line =~ /((\(\d{3}\)\s+|\d{3}-?|\d{3}\.?)(\d{3}-?|\d{3}\.?)\d{4})/g) {
        print "$1\n";
    }
} else {
    print "No match\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM