我有一个密码正则表达式,如下
^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*])).{0,100}$
我想添加正则表达式来验证这一点,并排除连续插入四个相同字符
我发现这个模式可以匹配 4 个相同的字符,但是我如何组合它们,并且这个模式应该检查是否有 4 个相同的字符,密码应该无效。
(.)\1{3}
最佳答案
这有效:
^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*]))((.)(?!\3{3})){0,100}$
说明:
^ // beginning of string
( // begin capturing group #1 (this is actually unnecessary)
(?=.*\d) // must contain a digit
(?=.*[a-z]) // must contain a lowercase letter
(?=.*[A-Z]) // must contain an uppercase letter
(?=.*[!@#$%&*]) // must contain a special character
) // end capturing group #1
( // begin capturing group #2: one character of password, for repetition quantifier
(.) // capturing group #3: one character of password, for negative lookahead
(?!\3{3}) // character is not followed by itself 3 times
) // end capturing group #2
{0,100} // repeat group #2 up to 100 times
$ // end of string
关于java - 将密码正则表达式与相同的字符连续组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29887626/