1
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 ..

nachikethas
  • 35
  • 1
  • 5

3 Answers3

5

You can do:

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

Perl documentation:

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

Igor
  • 26,650
  • 27
  • 89
  • 114
4

The other answers are correct, but if you ever want to negate a regex (and not the operator that applies it), you can use

/^(?!.*?$regex_to_be_negated)/s
moritz
  • 12,710
  • 1
  • 41
  • 63
0

Also:

unless ($USAGE=~/^\d{15}\b/)
{
  print "\nUSAGE is Invalid\n";
  print "length of $USAGE is ",length($USAGE);  
}
dsm
  • 10,263
  • 1
  • 38
  • 72