How to insert the whole matched text in the replacement in Perl?

回眸只為那壹抹淺笑 提交于 2021-02-05 12:20:48

问题


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


回答1:


Use $&, see http://perldoc.perl.org/perlvar.html

echo 'aabbcc' | perl -p -e 's/bbc/$&A/g'

aabbcAc



来源:https://stackoverflow.com/questions/45328424/how-to-insert-the-whole-matched-text-in-the-replacement-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!