我需要帮助在特殊字符之前添加反斜杠。 注意:我不能使用任何模块,所以我需要创建简单的脚本。
假设你有这样的行:
<link class="include" rel="stylesheet" type="text/css" href="../css/style.css" />
现在我想在任何“或其他特殊的Perl字符之前添加”\“
$%/!`|
最后它应该是:
<link class=\"include\" rel=\"stylesheet\" type=\"text/css\" href=\"../css/style.css\" />
我试图使用:
$line =~ s/["%'\/{|}]+/\\$1/g;
没用。
我应该使用什么样的正则表达式?
答案 0 :(得分:1)
您希望$1
中可以使用特殊字符。为此,您需要使用( )
$line =~ s/ ( ["%'\/{|}] ) /\\$1/xg;
注意我添加了一些间距和// x限定以使()更好地突出。
另一种方法是使用前瞻性。
$line =~ s/(?= [%'\/{|}] ) /\\/xg;