% cat t0 use strict; use warnings; my $input = 'foo.ra'; my $find = '\.(.)(.)$'; my $replace = 'b$2$1s'; printf qq{result "%s"\n}, replace($input, $find, $replace); exit 0; sub replace { my($input, $find, $replace) = @_; $input =~ s{$find}{qq{"$replace"}}ee or die "input did not match"; return $input } % perl t0 result "foobars" %