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);  
}

这按预期工作,但我怎样才能否定这个正则表达式?说如果使用不是/^\d{15}\b/

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

我试过这个,但它不工作..

4

3 回答 3

5

你可以做:

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

Perl 文档

二进制“!~”就像“=~”,除了返回值在逻辑意义上被否定。

于 2011-06-28T11:29:33.053 回答
4

其他答案是正确的,但是如果您想否定一个正则表达式(而不是应用它的运算符),您可以使用

/^(?!.*?$regex_to_be_negated)/s
于 2011-06-28T11:53:44.520 回答
0

还:

unless ($USAGE=~/^\d{15}\b/)
{
  print "\nUSAGE is Invalid\n";
  print "length of $USAGE is ",length($USAGE);  
}
于 2011-06-28T11:47:45.877 回答