my $var = "though test tot 1 2 3 tesset"; $var =~ s/(t.*?t)/($1 ne "test") ? "" : $1/ge; print $var; # prints: esoesset