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