我有一系列需要在URL字符串列表中匹配的子字符串。子字符串具有特殊字符,如'|','*',' - ','+'等。如果URL字符串包含该子字符串,我需要执行一些操作。但现在让我们只是说我会在控制台中打印“TRUE”。匹配具有特殊字符的字符串的Perl正则表达式
我是这样做的,首先从子串列表中读取并放入哈希表中。然后,我尝试对每个URL执行整个列表的简单正则表达式匹配,直到找到匹配项。代码是这样的。
open my $ADS, '<', $ad_file or die "can't open $ad_file";
while(<$ADS>) {
chomp;
$ads_list_hash{$lines} = $_;
$lines ++;
}
close $ADS;
open my $IN, '<', $inputfile or die "can't open $inputfile";
my $first_line = <$IN>;
while(<$IN>) {
chomp;
my @hhfile = split /,/;
for my $count (0 .. $lines) {
if($hhfile[9] =~ /$ads_list_hash{$count}/) {
print "$hhfile[9]\t$ads_list_hash{$count}\n";
print "TRUE !\n";
last;
}
}
}
close $IN;
的问题是,子有很多,这是造成在比赛$hhfile[9] =~ /$ads_list_hash{$count}/
错误的特殊字符。几个例子是;
+adverts/
.to/ad.php|
/addyn|*|adtech;
我得到这样的行中的错误,基本上说“量词在正则表达式中没有任何含义”。我是否需要在正则表达式匹配语法中查找某些内容以避免这些?
如果你只是想找到一个字符串,然后一个正则表达式似乎有点矫枉过正...有一个原因[指数](http://perldoc.perl.org/functions/index.html)没有做你所需要的? – jswolf19 2011-03-25 13:36:16
[我如何处理Perl正则表达式中的特殊字符?](http://stackoverflow.com/questions/576435/how-do-i-handle-special-characters-in-a-perl-regex) – daxim 2011-03-25 14:15:47