my $text =~ s[(?<=BEGIN_MARKER)(.*?)(?=END_MARKER)]{ (my $rep = $1) =~ s[i][x]g; # For single char subs you could use tr// here $rep; }ge; print $text; An abitrary BEGIN_MARKER strxng whxch END_MARKER could contain BEGIN_MARKER just about END_MARKER anything