Perl 正则表达式匹配具有特殊字符的字符串 [英] Perl regexp matching for strings with special characters

查看:111
本文介绍了Perl 正则表达式匹配具有特殊字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要在 URL 字符串列表中匹配的子字符串列表.子字符串具有特殊字符,如|"、*"、-"、+"等.如果 URL 字符串包含该子字符串,我需要进行一些操作.但是现在让我们说我将在控制台中打印TRUE".

I have list of substrings which I need to match within a list of URL strings. The substrings have special characters like '|', '*', '-', '+' etc. If the URL strings contains that substring I need to do some operation. But for now lets just say I will print "TRUE" in the console.

我首先从子字符串列表中读取并将其放入散列中.然后,我尝试对每个 URL 的整个列表执行简单的 Regexp 匹配,直到找到匹配项.代码是这样的.

I did this by first reading from the list of substrings and putting it into a hash. I then tried to perform a simple Regexp match of the entire list for each URL until a match is found. The code is something like this.

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}/.几个例子是;

The problem is that the substrings have a lot of special characters which is causing errors in the match $hhfile[9] =~ /$ads_list_hash{$count}/. Few examples are;

+adverts/
.to/ad.php|
/addyn|*|adtech;

我在这样的行中得到一个错误,它基本上说量词在正则表达式中没有任何内容".我是否需要更改正则表达式匹配语法中的某些内容以避免这些?

I get an error in lines like these which basically says "Quantifier follows nothing in regexp". Do I need to chanhge something in the regexp matching syntax to avoid these?

推荐答案

您需要对字符串中的特殊字符进行转义.

You need to escape the special characters in the string.

\Q\E 之间的字符串括起来就可以了:

Enclosing the string between \Q and \E will do the job:

if($hhfile[9] =~ /\Q$ads_list_hash{$count}\E/) {

这篇关于Perl 正则表达式匹配具有特殊字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆