[英]Regular expression for password validation in java
我已经编写了此正则表达式,需要针对Java中的一组规则进行测试。 规则是:
这是我写的正则表达式。 [a-zA-Z\\w\\D][a-zA-Z0-9\\w][a-zA-Z0-9].$
它有时有效,有时却无效。 而且我不知道为什么! 我非常感谢您为实现这一目标所提供的帮助。
尝试这个:
Pattern pattern = Pattern.compile(
"(?=.*[A-Z])" + //At least one upper case character (A-Z)
"(?=.*[a-z])" + //At least one lower case character (a-z)
"(?=.*\\d)" + //At least one digit (0-9)
"(?=.*\\p{Punct})" + //At least one special character (Punctuation)
"^[^\\d]" + // Password should not start with a digit
".*" +
"[a-zA-Z\\d]$"); // Password should not end with a special character
Matcher matcher = pattern.matcher("1Sz1");
System.out.println(matcher.matches());
试试这个:
^[a-zA-Z@#$%^&+=](?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).{8,}[a-zA-Z0-9]$
说明:
^ # start-of-string
[a-zA-Z@#$%^&+=] # first digit letter or special character
(?=.*[0-9]) # a digit must occur at least once
(?=.*[a-z]) # a lower case letter must occur at least once
(?=.*[A-Z]) # an upper case letter must occur at least once
(?=.*[@#$%^&+=]) # a special character must occur at least once
.{8,} # anything, at least eight places though
[a-zA-Z0-9] # last digit letter or number
$ # end-of-string
这种模式使添加或删除规则变得容易。
此答案归功于以下两个主题:
和
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.