my $re = qr/(?!\A.*38[0-9]{8}[A-Z]{3}.*\z)/; if ($reference =~ m/$re/) { # 38nnnnnnnnXXX pattern was not found $reference = ""; }