Jānis Elmeris Jānis Elmeris - 1 year ago 69
Perl Question

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

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

Answer Source

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

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

aabbcAc

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download