Use variable as RegEx pattern

后端 未结 2 1012
礼貌的吻别
礼貌的吻别 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.

    0 讨论(0)
  • 2021-02-07 00:51

    You cannot use \Q in a single-quoted / non-interpolated string. It must be seen by the lexer.

    Anyway, tilde isn’t a meta-character.

    Add use regex "debug" and you will see what is actually happening.

    0 讨论(0)
提交回复
热议问题