Search code examples
regexperlnegate

Negating a perl regexp


my ($INV_NB, $USAGE)=split /\|/,"9998|999999999999999";

if ($USAGE=~/^\d{15}\b/)
{
  print "\nUSAGE is Valid\n";
  print "length of $USAGE is ",length($USAGE);  
}

This worked as expected, but how can I negate this regexp? say if usage is not /^\d{15}\b/

if ($USAGE!=~/^\d{15}\b/)
{
  print "\nUSAGE is Invalid\n";
  print "length of $USAGE is ",length($USAGE);  
}

I tried this, but it isnt working ..


Solution

  • You can do:

    if ($USAGE !~ /^\d{15}\b/)
    

    Perl documentation:

    Binary "!~" is just like "=~" except the return value is negated in the logical sense.