in reply to Re: find a substring using regex and then replace it
in thread find a substring using regex and then replace it

It's unlikely but not beyond the realms of possibility to have more than ten interfaces so it would be safer to match for one or more (+) digits at the end of the string. This gives the wrong result:-

$ perl -Mstrict -Mwarnings -E ' my $interface = q{bge12}; $interface =~ s{[0-9]$}{:$&}; say $interface;' bge1:2 $

This corrects the problem. I also prefer to use the \d special escape for digits and a look-ahead to save having to mention what was matched in the replacement part:-

$ perl -Mstrict -Mwarnings -E ' my $interface = q{bge12}; $interface =~ s{(?=\d+$)}{:}; say $interface;' bge:12 $

I hope this is of interest.

Cheers,

JohnGG