$_ = 'foo bah bar'; $find = 'bah'; $replace = '[\1]'; s/(\Q$find\E)/$replace/g; $\ = "\n"; print; # "foo [\1] bar" and not "foo [bah] bar" #### $_ = 'foo bah bar'; $find = 'bah'; $replace = '[\1]'; $idx = index($_, $find); substr($_, $idx, length($find), $replace) if $idx >= 0; $\ = "\n"; print; #### $_ = 'foo bah bah bar'; $find = 'bah'; $replace = '[\1]'; $idx = length($_); while (($idx = rindex($_, $find, $idx-1)) >= 0) { substr($_, $idx, length($find), $replace); } $\ = "\n"; print;