.net 正则表达式不等于字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2964645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex not equal to string
提问by Kieron
I'm banging my head against a wall with a regular expression. I'm trying to define an expression that excludes exactly this text 'System' (case insensitive), but can contain the word 'System' providing it's not just that.
我用正则表达式把头撞在墙上。我正在尝试定义一个表达式,该表达式完全排除此文本“系统”(不区分大小写),但可以包含“系统”一词,前提是它不仅如此。
Examples:
例子:
- System == INVALID
- SYSTEM == INVALID
- system == INVALID
- syStEm == INVALID
- asd SysTem == Valid
- asd System asd == Valid
- System asd == Valid
- asd System == Valid
- asd == Valid
- 系统 == 无效
- 系统 == 无效
- 系统 == 无效
- 系统 == 无效
- asd 系统 == 有效
- asd 系统 asd == 有效
- 系统 asd == 有效
- asd 系统 == 有效
- asd == 有效
回答by Kobi
Try this:
尝试这个:
^(?!system$)
Or this to match the whole line:
或者这样匹配整行:
^(?!system$).*$
The regex has a negative look-ahead on its beginning, which doesn't match if "system" is the entire string.
正则表达式在其开头有一个否定的前瞻,如果“system”是整个字符串,则它不匹配。
回答by Amarghosh
Reject if it matches ^system$(make sure iflag is ON).
如果匹配则拒绝^system$(确保i标志为 ON)。
回答by Steve Bennett
^$|^.{1-5}$|.{7}|^[^s]|^.[^y]|^..[^s]|^...[^t]|[^e].$|[^m]$
But use amarghosh's answer if you can.
但如果可以,请使用 amarghosh 的答案。
(updated as per suggestion below)
(根据以下建议更新)

