这是我的代码:
$html = 'Is this a question? Maybe.';
$old = 'question?';
$new = 'reply?';
$html =~ s/$old/$new/g;
print $html; exit;
输出是:
Is this a reply?? Maybe.
期望的输出:
Is this a reply? Maybe.
我做错了什么?
最佳答案
使用quotemeta转义 ?
:
$html = 'Is this a question? Maybe.';
$old = quotemeta 'question?';
$new = 'reply?';
$html =~ s/$old/$new/g;
print $html; exit;
关于regex - 替换包含问号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449452/