use strict; use warnings; my $A="abc\ndef\nghi\n"; # remember matching is greedy but # without /s modifier '.' does not match \n my $B = join('', grep { ! /def/ } $A =~ /(.*\n)/g); print $B;