A positive look behind
$string="chocolate/cake/milk/ice cream."; $string =~ s/(?<= chocolate) milk/ candy/; print "$string\n"; $string="this is a test."; $string =~ s/(?<=a test) is/ is not/; print "$string\n";