Perl Regex不区分大小写

时间:2018-04-17 15:03:16

标签: regex perl

我确定我错过了一些简单的事情。下面的代码我试图匹配文件的两行与正则表达式不敏感(// i)。如果文件行在不同情况下不相同,它仍然匹配。

没有模式匹配,遍历文件并逐行比较,它工作正常。只是试图添加不区分大小写。

#add file lines to array for comparison
while (my $fileLine = <FILE1>)
{
   chomp($fileLine);
   push @lines, $fileLine;
   $compare{$fileLine}++;
}
#comparison of second file to first file
while (my $fileLine2 = <FILE2>)
{

   chomp($fileLine2);
   $lineNum = 1 + $count;
   my $comp = $fileLine2;

   if ($comp =~ s/$lines[$count]//i)
   {
     print "The different line is at Line Number: $lineNum \n" unless(exists($compare{$fileLine2}));
     print "File A: $fileLine2\n" unless (exists($compare{$fileLine2}));
     print "File B: $lines[$count]\n" unless (exists($compare{$fileLine2}));
   }

}

1 个答案:

答案 0 :(得分:3)

您的代码不匹配,取而代之的是:

population_df["population"] = population_df[["country"]].applymap(lambda x: population_dict[x])

$comp =~ s/$lines[$count]//i 被解释为正则表达式,但您将字符串存储到数组中。并非每个字符串都匹配从它创建的正则表达式,例如

$lines[$count]

my $string = 'a+b'; $string =~ s/$string//; # $string is unchanged 作为正则表达式意味着至少有一个a+b后跟a ,这显然不适用于该字符串。请参阅quotemeta

您还没有显示出遇到问题的行,但您可以看到它即使没有区分大小写也无法正常工作。

要比较$ s1和$ s2是否相同,无论如何,都可以使用

b

对于unicode casefolding,请参阅lcucfc

您还需要将统一字符串存储在哈希值中!