Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 不区分大小写的正则表达式匹配在perl中不起作用_Regex_Perl_Csv_Case - Fatal编程技术网

Regex 不区分大小写的正则表达式匹配在perl中不起作用

Regex 不区分大小写的正则表达式匹配在perl中不起作用,regex,perl,csv,case,Regex,Perl,Csv,Case,perl中的正则表达式模式正确匹配区分大小写的字符串,但不匹配大小写不同的字符串。我正在解析一个CSV文件,其中第一行是国家名称,其他行是该国家的缩写或常见的其他拼写 示例:CSV的第1列是美国、美国、美国。第二栏是:墨西哥、墨西哥、墨西哥 以下是完整的代码:: #!/usr/bin/perl use strict; use warnings; use Data::Dumper qw(Dumper); my $filename = 'countrycodes.csv'; my $li

perl中的正则表达式模式正确匹配区分大小写的字符串,但不匹配大小写不同的字符串。我正在解析一个CSV文件,其中第一行是国家名称,其他行是该国家的缩写或常见的其他拼写

示例:CSV的第1列是美国、美国、美国。第二栏是:墨西哥、墨西哥、墨西哥

以下是完整的代码::

    #!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper qw(Dumper);

my $filename = 'countrycodes.csv';
my $line;
my @rowStrings;
my @rows;
my @columns;
这是我用来测试代码的字符串:

my $string = "Mex, MEX, USA, usa, US, MX, CAN, Canada";

open(my $fh, '<', $filename) or die "Can't open $filename: $!";

$line = <$fh>;
@rowStrings = split("\r", $line);

#make rows strings into arrays
foreach my $i (0..$#rowStrings){
    $rows[$i] = [split(",",$rowStrings[$i])];
}


my $columnCount = values scalar $rows[0];

print "column count: $columnCount \n";

#create array for each column from CSV
foreach my $column (0..$columnCount){
    foreach my $row (0..$#rows){
        $columns[$column][$row] =  $rows[$row][$column];
        if ($columns[$column][$row]) {
        }
    }

}
这是作为最终结果的终端输出:

Mex, Mexico, United States, usa, United States, Mexico, Canada, Canada
如您所见,MEX匹配正确,因为这是它正在搜索的术语,但不是MEX,即使我正在使用/I修改器。我做错了什么

编辑:匹配的是美国,而不是美国

作为参考,正则表达式模式是
$string=~s/\s$columns[$col][$ro],/$head,/i


谢谢大家!

我不完全理解您在做什么,但这可能会有所帮助:正则表达式中的\s尝试匹配空白,但不会匹配空白。因为您的“Mex”位于行的开头,所以它前面没有空格。作为实验,尝试将“Mex”移动到行中的其他位置。

似乎解析CSV不是您的问题。(我仍然推荐
Text::CSV

假设您在一个数组中有您的语言和备选方案,并且您有一个包含这些语言和备选方案数组的数组,那么您可以只比较输入。您可能应该去掉前导和尾随空格,并比较不区分大小写,但不需要正则表达式:

#!/usr/bin/perl
use strict;
use warnings;

my @countries = (   
    ['United States of America', 'US', 'USA', 'US of A', 'United States'],
    ['Mexico', 'MX', 'Mex'], 
);

my @input = ('US ', '  mx   ', ' Mexico', ' us of a');

foreach my $input (@input) {  
    $input =~ s/^\s+//;
    $input =~ s/\s+$//; 
    my $found = 0;
    foreach my $country (@countries) {  
        foreach my $alternative (@$country) {
            if (lc($input) eq lc($alternative)) {  
                print "$input is ${$country}[0]\n";
                $found = 1;
            }
        } 
    }   
    print "did not find $input\n" unless($found);
}

问题是我没有包括“g”操作符,这意味着一旦它找到一个国家名称替代实例,它就不再寻找其他国家名称


通过将
$string=~s/\s$columns[$col][$ro],/$head,/i
更改为
$string=~s/\s$columns[$col][$ro],/$head,/ig
,匹配是正确的。

为什么不打印
$columns[$coll][$ro]
以查看它试图匹配的内容。我一开始就打印出来了。我知道它与CSV字段的确切拼写相匹配,但在大小写不同的情况下不匹配。可能Mex不匹配,因为它前面没有空格,而您正在专门搜索那里的空格。
\s
。美国呢?我不能把所有选项都去掉,因为此CSV还包括可能包含这些字符的人名。
#!/usr/bin/perl
use strict;
use warnings;

my @countries = (   
    ['United States of America', 'US', 'USA', 'US of A', 'United States'],
    ['Mexico', 'MX', 'Mex'], 
);

my @input = ('US ', '  mx   ', ' Mexico', ' us of a');

foreach my $input (@input) {  
    $input =~ s/^\s+//;
    $input =~ s/\s+$//; 
    my $found = 0;
    foreach my $country (@countries) {  
        foreach my $alternative (@$country) {
            if (lc($input) eq lc($alternative)) {  
                print "$input is ${$country}[0]\n";
                $found = 1;
            }
        } 
    }   
    print "did not find $input\n" unless($found);
}