use strict; use warnings; my $A = "abc\ndef\nghi\n"; while ($A =~ /(.+)/g) { print "$1\n" unless $1 =~ /def/; }