my $string = "ERROR blah license will expire"; if ($string =~ /^ERROR\b.+?(?:Error processing Cancel Execution|license will expire)\b/m) { print "NOT MATCHED\n"; } else { print "MATCHED\n"; } #### if ($string =~ /^ERROR\b/m and $string !~ /\b(?:Error processing Cancel Execution|license will expire)\b/) { print "MATCHED\n"; } else { print "NOT MATCHED\n"; }