如果你改变
my $re_str_q = '\Q1\E'; #from
my $re_str_q = qr/\Q1\E/; #to
这将是传递动态生成的正则表达式的正确方法,那么它将给出以下结果
re_str match: 1
re_str_q match: 1
direct match: 1
另外,如果您使用过
use strict;
use warnings;
你会收到警告
Unrecognized escape \Q passed through in regex; marked by <-- HERE in m/\Q <-- HERE 1\E/ at so.pl line 9.
Unrecognized escape \E passed through in regex; marked by <-- HERE in m/\Q1\E <-- HERE / at so.pl line 9.
这会给你一些关于出了什么问题的迹象。
更新
要更详细地了解这一点,您可以从此处阅读
从参考文件中取出
以下转义序列在 . 的构造中可用interpolate
,但在transliterations
.
\l lowercase next character only
\u titlecase (not uppercase!) next character only
\L lowercase all characters till \E or end of string
\U uppercase all characters till \E or end of string
\F foldcase all characters till \E or end of string
\Q quote (disable) pattern metacharacters till \E or
end of string
\E end either case modification or quoted section
(whichever was last seen)
请参阅quotemeta以了解 \Q 引用的字符的确切定义。
\L 、 \U 、 \F 和 \Q 可以堆叠,在这种情况下,您需要一个 \E 。例如:
say"This \Qquoting \ubusiness \Uhere isn't quite\E done yet,\E is it?";
This quoting\ Business\ HERE\ ISN\'T\ QUITE\ done\ yet\, is it?