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"