2012-03-13 91 views

回答

1

通常你會喜歡

(?!ABC) 

因此,例如:

^(?!ABC$).* 

所有這一切AREN字符串」 t ABC

分解它意味着:

^ beginning of the string 
(?!ABC$) not ABC followed by end-of-string 
.* all the characters of the string (not necessary to terminate it with $ because it is an eager quantifier) 

從技術上講,你可以做類似

^.*(?<!^ABC)$ 

分解後它使用後面的負看看意味着

^ beginning of the string 
.* all the characters of the string 
(?<!^ABC) last three characters captured aren't beginning-of-the-string and ABC 
$ end of the string (necessary otherwise the Regex could capture `AB` of `ABC` and be successfull) 

,但它更復雜的閱讀(寫)

啊,顯然不是所有的正則表達式實現都實現它們:-) .NET就是這樣做的。

+0

@Tim你是對的。 – xanatos 2012-03-13 14:36:34

+0

作品。與^(?! ABC $)。* – Skuli 2012-03-13 15:19:52

0

因爲有許多正則表達式的風格,所以很難在不知道你使用什麼語言的情況下明確地回答這個問題,但是你可以用負面預測來做到這一點。

https://stackoverflow.com/a/164419/1112402

+0

我需要這個在asp.net mvc 3中的正則表達式模型驗證。 – Skuli 2012-03-13 14:58:28

0

(?!ABC)^。 $
試試這個,這會排除所有包含ABC的字符串。

相關問題