假設我想匹配除1之外的所有字符串:「ABC」 我該怎麼做?正則表達式來判斷一個字符串是否不是特定的字符串
我需要這個了regular expression model validation在asp.net mvc的3
假設我想匹配除1之外的所有字符串:「ABC」 我該怎麼做?正則表達式來判斷一個字符串是否不是特定的字符串
我需要這個了regular expression model validation在asp.net mvc的3
通常你會喜歡
(?!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就是這樣做的。
因爲有許多正則表達式的風格,所以很難在不知道你使用什麼語言的情況下明確地回答這個問題,但是你可以用負面預測來做到這一點。
我需要這個在asp.net mvc 3中的正則表達式模型驗證。 – Skuli 2012-03-13 14:58:28
(?!ABC)^。 $
試試這個,這會排除所有包含ABC的字符串。
@Tim你是對的。 – xanatos 2012-03-13 14:36:34
作品。與^(?! ABC $)。* – Skuli 2012-03-13 15:19:52