$a = "fox comes and fox goes into forest"; $a =~ s#(fox.*?)(fox.*?forest)#$1the $2#gsi; print $a;