Use variable as RegEx pattern

后端 未结 2 1013
礼貌的吻别
礼貌的吻别 2021-02-07 00:23

I\'d like to use a variable as a RegEx pattern for matching filenames:

my $file = \"test~\";
my $regex1 = \'^.+\\Q~\\E$\';
my $regex2 = \'^.+\\\\Q~\\\\E$\';
prin         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 00:40

    As documentation says:

        $re = qr/$pattern/;
        $string =~ /foo${re}bar/; # can be interpolated in other patterns
        $string =~ $re; # or used standalone
        $string =~ /$re/; # or this way
    

    So, use the qr quote-like operator.

提交回复
热议问题