简体   繁体   English

Java Regex用于密码验证

[英]Java Regex for password validation

I have created regex for password validation in a Java app. 我已经在Java应用程序中创建了用于密码验证的正则表达式。 The regex I created is below for below requirement. 我为以下需求创建的正则表达式如下。

^[\\p{Alnum}#.!@$*&_]{5,12}$
  1. At least 5 chars and max 12 chars 至少5个字符,最多12个字符
  2. Contains at least one digit 包含至少一位数字
  3. Contains at least one char 包含至少一个字符
  4. may contain chars within a set of special chars (#.!@$*&_) 可能包含一组特殊字符(#.!@$*&_)中的字符
  5. Does not contain space, tab, etc. 不包含空格,制表符等。

I am missing 1 and 2 like if I give "gjsajhgjhagfj" or "29837846876", it should fail. 我缺少1和2,就像如果我给“ gjsajhgjhagfj”或“ 29837846876”一样,它应该失败。 But it's not happening. 但这没有发生。

Could anyone help please? 有人可以帮忙吗?

You can use lookaheads to force conditions 1 & 2: 您可以提前使用条件1和2:

^(?i)(?=.*[a-z])(?=.*[0-9])[a-z0-9#.!@$*&_]{5,12}$

DEMO 演示

(?i) is for insensitive match, the same as Pattern.CASE_INSENSITIVE flag for your compile function: (?i)用于不敏感的匹配,与您的编译函数的Pattern.CASE_INSENSITIVE标志相同:

Pattern.compile(regex, Pattern.CASE_INSENSITIVE)

You can read more about Lookaheads HERE 您可以在此处详细了解Lookaheads

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

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