2011-06-28 40 views
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/否定一個perl正則表達式

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

我嘗試這樣做,但它不是工作..

+0

使用代碼中的選項卡是禁忌的。 – 2011-06-28 11:23:49

+0

還有'除非(EXPR)BLOCK',它可能會使意義更明顯。 – Mike

回答

5

你可以這樣做:

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

Perl documentation

二進制「!〜」就像「=〜」,除了 返回值在邏輯意義上被否定。

+1

或者你可以做'除非($ USAGE =〜/^\ d {15} \ b /)'或'if(不是$ USAGE =〜/^\ d {15} \ b / –

0

另外:

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

其他的答案是正確的,但如果你想否定一個正則表達式(而不是應用它的運營商),你可以使用

/^(?!.*?$regex_to_be_negated)/s