简体   繁体   English

正则表达式验证密码

[英]Password validation by regex

System.out.println(Pattern.matches("[A-Z]{1,}[a-z]{1,}[0-9]{2,}[@#$%]+[ ]*", "DEFabc45$  "));

but it don't work When I pass string like passW@ord45 . 但是当我传递诸如passW@ord45类的字符串时它passW@ord45 Mean I should not following regex order pattern written here. 意味着我不应该遵循此处编写的正则表达式订购模式。

Following are the conditions given bellow: 以下是给定的条件:

Write a function boolean isValidPassword (String password) A Password is valid if it satisfies following conditions: 编写函数boolean isValidPassword (String password)如果满足以下条件,则密码有效:

  1. Length of the password must be more than 10 (without counting spaces). 密码长度必须大于10(不包含空格)。
  2. Password must contain at least one capital and one small alphabet 密码必须至少包含一个大写字母和一个小写字母
  3. Password must contain at least 2 digits and if it does not have 2 digits then see next condition. 密码必须至少包含2位数字,如果没有2位数字,请参阅下一个条件。
  4. Password must contain at least one special character from the given set- {$, @, _, -, ., /} 密码必须包含给定集合中的至少一个特殊字符-{$,@,_,-,。,/}

You can use the method linked in @davide comment, so basically: 您可以使用@davide注释中链接的方法,因此基本上是:

(?=(?: *\\S){10})(?=.*[a-z])(?=.*[A-Z])(?=.*[$@_./-]|.*[0-9].*[0-9])

However, using .* everywhere will produce a lot of backtracking (in particular .*[0-9].*[0-9] ), so the way to prevent this is to be more explicit: 但是,在任何地方使用.*都会产生很多回溯(特别是.*[0-9].*[0-9] ),因此,防止这种情况的方法应该更加明确:

(?=(?: *\\S){10,})(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])[^$@_./0-9-]*+(?:[$@_./-]|[0-9][^0-9]*[0-9])

About features used in these two patterns: 关于这两种模式中使用的功能:

\\\\S is a shorthand character class: all that is not a white-space character \\\\S是速记字符类: 不是空格字符的所有字符

(?=...) is a zero-width assertion (a test) called lookahead, and means followed by . (?=...)是一个称为先行的零宽度断言(测试), 其后跟 So with it you can test several assertions from the same position like this: (?=..)(?=..)(?=..)... since characters are not consumed. 因此,可以使用它从同一位置测试多个断言,例如: (?=..)(?=..)(?=..)...因为不会消耗字符。

*Note that in the second pattern, I have choosen to not use the lookahead because since there are no more rules, it's not important to consume characters or not. *请注意,在第二种模式中,我选择不使用超前功能,因为由于没有更多规则,因此是否使用字符并不重要。

So each lookahead contains a rule: 因此,每个前瞻都包含一个规则:

rule 1: at least 10 non-space characters 规则1:至少10个非空格字符

(?=
    (?: *\\S){10}  # a group with optional spaces and one non space character
                   # the group is repeated 10 times, so there are at least
                   # 10 characters that are not a space
)

rule 2a: a lowercase letter 规则2a:小写字母

(?=
    [^a-z]*    # zero or more characters that are not a lowercase letter
    [a-z]      # until a lowercase letter
)

rule 2B: an uppercase letter, (same as 2a but with AZ ) 规则2B:大写字母(与2a相同,但带AZ

rules 3 et 4 are combined in one subpattern that is not in a lookahead as explained: 如前所述,规则3和4被组合到一个不在超前的子模式中:

[^$@_./0-9-]*+          # all that is not a digit or a special character
                        # Note: I have added a possessive quantifier make the whole
                        # pattern to fail faster if the next group fails 
(?: # two possibilities:
    [$@_./-]            # a special character
  |                  # OR
    [0-9][^0-9]*[0-9]   # two digits: a digit, other characters, an other digit 
) 

You can use these patterns as it with lookingAt since there is no need to anchor them at the end (according to your requirements) or with matches but you need to add .* at the end to reach the end of the string. 您可以在lookingAt使用这些模式,因为不需要在末尾(根据您的要求)或matches将其锚定,但是您需要在末尾添加.*以到达字符串的末尾。

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

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