简体   繁体   English

用于连续重复字母、数字和特殊字符的 Java 正则表达式

[英]Java regex for consecutively repeated letters, numbers and special characters

I have to apply a password policy and I am using this Regex (default to my Identity Server) which accepts password as a combination of lower case, upper case, number and special character:我必须应用密码策略,并且我正在使用这个正则表达式(默认为我的身份服务器),它接受密码作为小写、大写、数字和特殊字符的组合:

^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*])).{0,100}$

I need to modify it so that it should not match string having more than 3 consecutive copies of the same character, as in eg Adminnnn@123 .我需要修改它,使其不应该匹配具有超过 3 个相同字符的连续副本的字符串,例如Adminnnn@123

That was tricky, but I think this should work ( try it out live here ):这很棘手,但我认为这应该可行(在这里现场尝试):

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*])(?!.*(.)\1\1\1)[0-9a-zA-Z!@#$%&*]{0,100}$

I am using a 4 lookahead assertions and one negative lookahead assertion.我使用了 4 个前瞻断言和一个否定前瞻断言。

(?=.*[0-9])         must contain a number 
(?=.*[a-z])         must contain a lower case
(?=.*[A-Z])         must contain an upper case
(?=.*[!@#$%&*])     must contain a special character
(?!.*(.)\1\1\1)     must not repeat the character in group 1 more than 3 times
[0-9a-zA-Z!@#$%&*]  is composed of these characters
{0,100}             0 to 100 symbols allowed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM