如何在 Perl 的替换中插入整个匹配的文本? [英] How to insert the whole matched text in the replacement in Perl?
本文介绍了如何在 Perl 的替换中插入整个匹配的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在所有特定的其他字符串 (bbc
) 之后添加一个字符串 (A
).所以,我匹配 bbc
并想用附加的 A
替换它自己('aabbcc' => 'aabbcAc'
).>
是否有替换后向引用被整个匹配替换?
$0
似乎不起作用——它的内容总是-e",出于某种原因:<代码>$ echo 'aabbcc' |perl -p -e 's/bbc/$0A/g'氨基酸
解决方案
使用 $&
,参见 http://perldoc.perl.org/perlvar.html
echo 'aabbcc' |perl -p -e 's/bbc/$&A/g'
<块引用>
aabbcAc
I want to add a string (A
) after all specific other strings (bbc
). So, I match bbc
and want to replace it with itself with A
appended ('aabbcc' => 'aabbcAc'
).
Is there a replacement back-reference that gets substituted with the whole match?
$0
doesn't seem to work – its content is always "-e", for some reason:
$ echo 'aabbcc' | perl -p -e 's/bbc/$0A/g'
aa-eAc
解决方案
Use $&
, see http://perldoc.perl.org/perlvar.html
echo 'aabbcc' | perl -p -e 's/bbc/$&A/g'
aabbcAc
这篇关于如何在 Perl 的替换中插入整个匹配的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文