Regex 使用perl将字符串替换为特殊字符

Regex 使用perl将字符串替换为特殊字符,regex,linux,perl,special-characters,Regex,Linux,Perl,Special Characters,我正在尝试替换一个字符串,比如 </string§> 在Linux上使用perl 问题是,它不会接受以及/ 这就是我得到的: perl -i -pe 's/\<\/string§\>/new string line 1 \n new string line 2/se' file.xml perl-i-pe's/\/new string line 1\n new string line 2/se'file.xml 有人能帮忙吗 谢谢 您不需要s和e修饰符,也可以使用备

我正在尝试替换一个字符串,比如

</string§>
在Linux上使用perl

问题是,它不会接受<和>以及/

这就是我得到的:

perl -i -pe 's/\<\/string§\>/new string line 1 \n new string line 2/se' file.xml
perl-i-pe's/\/new string line 1\n new string line 2/se'file.xml
有人能帮忙吗


谢谢

您不需要
s
e
修饰符,也可以使用备用正则表达式除雾器(除了
/
)来避免转义:

perl -pe 's~</string§>~new string line 1 \nnew string line 2~' file.xml
new string line 1
new string line 2
perl-pe的~~新的字符串行1\n新的字符串行2~'file.xml
新字符串行1
新字符串行2

谢谢,非常好用!另一个问题:你知道一个好的perl教程吗?@JakobBenz,你的替换在perl脚本中会很好地工作。你和你的外壳发生了冲突。一行中的反斜杠转义可能很有趣。在Perl看到反斜杠之前,必须让它通过shell。阿努巴瓦有完全避免它们的正确想法。
perl -pe 's~</string§>~new string line 1 \nnew string line 2~' file.xml
new string line 1
new string line 2