我有一个子字符串列表,我需要在URL字符串列表中匹配.子串具有特殊字符,如'|','*',' - ','+'等.如果URL字符串包含该子字符串,我需要执行一些操作.但是现在让我们说我会在控制台中打印"TRUE".
我这样做是首先从子串列表中读取并将其放入哈希值.然后,我尝试对每个URL执行整个列表的简单Regexp匹配,直到找到匹配项.代码是这样的.
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;
Run Code Online (Sandbox Code Playgroud)
问题是子串有很多特殊字符,导致匹配错误$hhfile[9] =~ /$ads_list_hash{$count}/.很少有例子;
+adverts/
.to/ad.php|
/addyn|*|adtech;
Run Code Online (Sandbox Code Playgroud)
我在这些行中得到一个错误,基本上说"量词在regexp中没有任何关系".我是否需要在正则表达式匹配语法中使用某些内容来避免这些?
cod*_*ict 14
您需要转义字符串中的特殊字符.
内附的串\Q并\E会做的工作:
if($hhfile[9] =~ /\Q$ads_list_hash{$count}\E/) {
Run Code Online (Sandbox Code Playgroud)