简体   繁体   中英

Java creating a complex regular expression for password validation

I have a regular expression right now that enforces a requirement on a password in my Java application.

I want to now modify this expression so it reflects this policy:

at least 7 characters contains characters in three or more of the following character classes:

(az), (AZ), (0-9), (@#$,. )

and the character at the beginning or end do not count towards its character class.

Is this too complex for a regular expression? If not, how can I modify my existing to adhere to the new one?

Thanks

Here is my current:

String credPattern = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%,.]).{7,})";
pattern = Pattern.compile(credPattern);
Matcher matcher = pattern.matcher(pw);

this pattern will apply all four character class conditions, beginning/end and minimum character count requirement:

^.(?=.*[a-z].)(?=.*[A-Z].)(?=.*[0-9].)(?=.*[@#$,.].).{6,}$

Demo
what you want to do is break it into four Regex patterns like so

^.(?=.*[a-z].)(?=.*[A-Z].)(?=.*[0-9].).{6,}$    
^.(?=.*[a-z].)(?=.*[A-Z].)(?=.*[@#$,.].).{6,}$  
^.(?=.*[a-z].)(?=.*[0-9].)(?=.*[@#$,.].).{6,}$  
^.(?=.*[A-Z].)(?=.*[0-9].)(?=.*[@#$,.].).{6,}$  

run each one and count the number of matches using your scripting language, if greater than 3 it's a success.
note: white spaces are allowed per your original pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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