kotlin 中的密码验证

Password Validation in kotlin

我需要一个正则表达式来验证密码 密码必须是: 长度为 8 个字符, 2个大写,3个小写,1个特殊字符和2个数字

([A-Z]{2})([?!@#*%^&-+]{1})([0-9]{2})([a-z]{3})$ 这是我想出的,但问题是它不符合任何顺序。

使用lookaheads:

^(?=(?:.*[a-z]){3})(?=(?:.*[A-Z]){2})(?=.*[?!@#*%^&-+])(?=(?:.*\d){2})[a-zA-Z?!@#*%^&-+\d]{8}$

解释:

^                       // Assert is the beginning of the string
(?=(?:.*[a-z]){3})      // Positive lookahead to match exactly 3 lowercase letters
(?=(?:.*[A-Z]){2})      // Positive lookahead to match exactly 2 uppercase letters
(?=.*[?!@#*%^&-+])      // Positive lookahead to match exactly 1 special character
(?=(?:.*\d){2})         // Positive lookahead to match exactly 2 numbers
[a-zA-Z?!@#*%^&-+\d]{8} // Assert that has exactly 8 of the defined characters
$                       // Assert is the end of the string

测试:RegExr

关于正则表达式知识的其他回复: