验证属性时将两个正则表达式组合为一个 [英] Combine two regular expression into one while validating Attribute
本文介绍了验证属性时将两个正则表达式组合为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个正则表达式.
-
[RegularExpression(@".*[^ ].*", ErrorMessage ="Something")]
验证仅包含空格的字符串(不包含其他任何字符) 例如:" ".length = 7
). -
[RegularExpression(@"^[^~!@#$%&*]+$", ErrorMessage = "something")]
验证包含~!@#$%&*
特殊字符的字符串.
[RegularExpression(@".*[^ ].*", ErrorMessage ="Something")]
validate string that only contains spaces(Not any other characters Ex:" ".length = 7
).[RegularExpression(@"^[^~!@#$%&*]+$", ErrorMessage = "something")]
validate string that contains~!@#$%&*
special characters.
我如何将两个正则表达式合并为一个,因为asp.net mvc中不允许使用重复的正则表达式注释.
How can I combine both regex into one, because Duplicate Regular expression annotation is not allowed in asp.net mvc.
推荐答案
您可以使用
^[^~!@#$%&*]*[^~!@#$%&*\s][^~!@#$%&*]*$
请参见 regex演示
详细信息
-
^
-字符串开头 -
[^~!@#$%&*]*
-除~!@#$%&*
列表中的字符外的0+个字符 -
[^~!@#$%&*\s]
-除~!@#$%&*
列表和空格中的字符外的其他字符 -
[^~!@#$%&*]*
-除~!@#$%&*
列表中的字符外的0+个字符 -
$
-字符串结尾.
^
- start of string[^~!@#$%&*]*
- 0+ chars other than a char in the~!@#$%&*
list[^~!@#$%&*\s]
- a char other than a char in the~!@#$%&*
list and whitespace[^~!@#$%&*]*
- 0+ chars other than a char in the~!@#$%&*
list$
- end of string.
注意:要同时允许使用空字符串,您需要在可选组中的锚点之间包装模式:^(?:[^~!@#$%&*]*[^~!@#$%&*\s][^~!@#$%&*]*)?$
.
NOTE: To also allow an empty string you need to wrap the pattern between the anchors within an optional group: ^(?:[^~!@#$%&*]*[^~!@#$%&*\s][^~!@#$%&*]*)?$
.
这篇关于验证属性时将两个正则表达式组合为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文