Perl 正则表达式 单引号
有人可以帮我运行下面的命令吗?我也尝试逃避单引号,但没有成功。
perl -pi.bak -e 's/Object\.prototype\.myString='q'//' myfile.html
Can someome help me to run the command below. I also tried to escape the single quotes but no luck.
perl -pi.bak -e 's/Object\.prototype\.myString='q'//' myfile.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在于 Perl,而在于你的 shell。要查看发生了什么,请尝试以下操作:
要使其正常工作,您可以将每个单引号替换为
'\''
,如下所示:来保存一些字符
或者您可以通过只写:甚至只写:
甚至:
双引号,正如 mu 建议的那样太短,在这里也可以工作,但在其他情况下可能会导致不必要的意外,因为 Perl 代码中常见的许多字符,如
$
、!
和 < code>\,即使在双引号内,对 shell 也有特殊含义。当然,另一种解决方案是将正则表达式中的单引号替换为八进制或十六进制代码
\047
或\x27
:The problem is not with Perl, but with your shell. To see what's happening, try this:
To make it work, you can replace each single quote with
'\''
, like this:or you can save a few characters by writing just:
or even just:
or even:
Double quotes, as suggested by mu is too short, will work here too, but can cause unwanted surprises in other situations, since many characters commonly found in Perl code, like
$
,!
and\
, have special meaning to the shell even inside double quotes.Of course, an alternative solution is to replace the single quotes in your regexp with the octal or hex codes
\047
or\x27
instead:双引号应该有效:
您可能需要也可能不需要在该正则表达式上使用
g
修饰符。之后您可能需要进行diff
以确保没有破坏 HTML。Double quotes should work:
You may or may not want a
g
modifier on that regex. And you'll probably want to do adiff
after to make sure you didn't mangle the HTML.