C# 除此字符外的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12422598/
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
Regular Expression Except this Characters
提问by d.Siva
I am using MVC data annotations and my requirement is that the address field can contain any characters (i.e. other than English characters are also allowed) except < > . ! @ # % / ? *.
我正在使用 MVC 数据注释,我的要求是地址字段可以包含除< > . ! @ # % / ? *.
I searched many sites but not getting how to write this regex.
我搜索了很多网站,但不知道如何编写这个正则表达式。
So far I have tried:
到目前为止,我已经尝试过:
[Required(ErrorMessage = "Address Required.")]
[RegularExpression(@"^[<>.!@#%/]+$", ErrorMessage = "Address invalid.")]
public string Address { get; set; }
采纳答案by Jens
Currently, you are only allowing string consisting ONLY of these letters.
目前,您只允许只包含这些字母的字符串。
Use
用
"^[^<>.!@#%/]+$"
回答by Aziz Shaikh
Try is regular expression:
尝试是正则表达式:
[^<>.!@#%/?*]
回答by Alessandro
This should do the work:
这应该做的工作:
"[^<>.!@#%/]"
"[^<>.!@#%/]"
EDIT:
编辑:
. (dot) is a reserved character in Regular Expressions, so you need to escape it.
. (dot) 是正则表达式中的保留字符,因此您需要对其进行转义。
回答by agentgonzo
Make your regex choose from any characters exceptthe ones listed with the caret:
让您的正则表达式从除插入符号列出的字符之外的任何字符中进行选择:
[^abc]
will match anything that's not an a, b, or c.
将匹配不是 a、b 或 c 的任何内容。
So putting it all together, your regex would be
所以把它们放在一起,你的正则表达式将是
^[^<>!@#%/?*]+$
Note here that the caret outside the square braces means 'match the start of the line', yet inside the square brackets means 'match anything that is notany of the following'
请注意,方括号外的插入符号表示“匹配行的开头”,而方括号内的意思是“匹配不属于以下任何一项的任何内容”

