Perl 编程/关键字/quotemeta
外观
这quotemeta命令返回表达式的值,使得所有 ASCII 非“单词”字符(与[A-Za-z_0-9]不匹配)都被反斜杠转义。这是实现双引号字符串中\Q转义的内部函数。如果表达式未传递,则该函数使用$_代替。
quotemeta EXPRESSION
代码
my $sentence = 'The quick brown fox jumpes over and over again.';
my $substring = 'quick.*?fox';
my $quoted_substring = quotemeta($substring);
$sentence =~ s{$quoted_substring}{big bad wolf};
print '"', $sentence, "\"\n\"", $substring, "\"\n\"", $quoted_substring, "\"\n";
返回以下内容
"The quick brown fox jumpes over and over again." "quick.*?fox" "quick\.\*\?fox"