regex - 替换包含问号的字符串

标签 regex perl

这是我的代码:

$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/

相关文章:

Java - 推文计数器

php - 从 PHP 中的字符串中删除除字母数字字符之外的所有内容

c - rm * 的 posix 正则表达式是什么?

perl - 如何使用 Perl 以字节数组形式读取图像?

Perl - 使用指针和索引进行二进制解包

perl - 使用 PAR Packer 进行编码时需要包含哪些内容

javascript - 匹配正则表达式条件

java - 替换标签内的字符串?

perl - 如何使用 MIME::Lite perl 检查上次发送的电子邮件是否已成功发送

perl - Perl 错误 "Can' t locate Net/SSH/Perl.pm”是什么意思?