use utf8; $text_before = "abc"; $text_after = "def"; $text =~ s/($text_before).($text_after)/$1X$2/