$var1="123x456x789xabc"; $var2=""; ($var2 = $var1) =~ s/((.*x){2})[^x]*?(x.*)/$1PROBE$2/;