问题
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