需要匹配正则表达式中至少3个给定模式

时间:2021-11-08 15:22:35

I am developing an application with MVC 4, razor. For one change Password view I need to do some validation which has following conditions:

我正在使用MVC 4开发一个应用程序razor。对于一个更改密码视图,我需要进行一些具有以下条件的验证:

It should match at least 3 of the following:

它应至少匹配以下3个:

1. Upper case alphabetic characters

1.大写字母字符

2. Lower case alphabetic characters

2.小​​写字母字符

3. Numbers

4. Special keyboard characters (except script tags)

4.特殊键盘字符(脚本标签除外)

Now, I have written following regular expression in my model

现在,我在我的模型中写了正则表达式

[RegularExpression("^([a-zA-Z0-9#$%=@!{},`~&*()'?.:;_|^/+\"-]{8,32})$", ErrorMessage = "Current Password is invalid")]

but, this will match any of the given subsets. I need to match atleast 3 of them. Can anybody please help me how do I do that?

但是,这将匹配任何给定的子集。我需要匹配其中至少3个。任何人都可以帮我,我该怎么做?

1 个解决方案

#1


1  

It might not be a good idea to use regex for password validation, but using lookaheads and the or operator | allows you to do this:

使用正则表达式进行密码验证可能不是一个好主意,但使用前瞻和or运算符允许你这样做:

^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])).{8,32}$

There are 4 parts in this regex:

这个正则表达式有4个部分:

(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]) 

Makes sure there's at least 1 lower alpha, 1 upper alpha and 1 number.

确保至少有1个较低的alpha,1个较高的alpha和1个数字。

(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])(?=.*[a-z])(?=.*[0-9]) 

Makes sure there's at least 1 special character, 1 upper alpha and 1 number.

确保至少有1个特殊字符,1个高位字母和1个数字。

(?=.*[A-Z])(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])(?=.*[0-9])

Makes sure there's at least 1 upper alpha, 1 special character and 1 number.

确保至少有1个上部字母,1个特殊字符和1个数字。

(?=.*[A-Z])(?=.*[a-z])(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])

Makes sure there's at least 1 upper alpha, 1 lower alpha and 1 special character.

确保至少有1个上部alpha,1个下部alpha和1个特殊字符。

And using | with those four (grouped into a non-capturing group) gives you the 3 out of 4 validation.

并使用|使用这四个(分组为非捕获组)为您提供4个验证中的3个。

Then use .{8,32} for the length of the password

然后使用。{8,32}作为密码的长度

#1


1  

It might not be a good idea to use regex for password validation, but using lookaheads and the or operator | allows you to do this:

使用正则表达式进行密码验证可能不是一个好主意,但使用前瞻和or运算符允许你这样做:

^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])).{8,32}$

There are 4 parts in this regex:

这个正则表达式有4个部分:

(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]) 

Makes sure there's at least 1 lower alpha, 1 upper alpha and 1 number.

确保至少有1个较低的alpha,1个较高的alpha和1个数字。

(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])(?=.*[a-z])(?=.*[0-9]) 

Makes sure there's at least 1 special character, 1 upper alpha and 1 number.

确保至少有1个特殊字符,1个高位字母和1个数字。

(?=.*[A-Z])(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])(?=.*[0-9])

Makes sure there's at least 1 upper alpha, 1 special character and 1 number.

确保至少有1个上部字母,1个特殊字符和1个数字。

(?=.*[A-Z])(?=.*[a-z])(?=.*[#$%=@!{},`~&*()'?.:;_|^/+\"-])

Makes sure there's at least 1 upper alpha, 1 lower alpha and 1 special character.

确保至少有1个上部alpha,1个下部alpha和1个特殊字符。

And using | with those four (grouped into a non-capturing group) gives you the 3 out of 4 validation.

并使用|使用这四个(分组为非捕获组)为您提供4个验证中的3个。

Then use .{8,32} for the length of the password

然后使用。{8,32}作为密码的长度