my $exceptions = qr/_ALPHA|#ALPHA|XL5 ALPHA/; my $keepers = qr/ALPHA|BETA/; while (my $srch = ) { print "Matched ($srch)!\n" unless $srch=~/^(?:$exceptions|(?!$keepers).)*$/; } __DATA__ one with #ALPHA and good ALPHA one with just XL5 ALPHA one with just BETA one with _ALPHA and BETA