cod*_*ict 398
您可以使用正向预测断言进行这些检查:
^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
Run Code Online (Sandbox Code Playgroud)
说明:
^ Start anchor
(?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters.
(?=.*[!@#$&*]) Ensure string has one special case letter.
(?=.*[0-9].*[0-9]) Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8} Ensure string is of length 8.
$ End anchor.
Run Code Online (Sandbox Code Playgroud)
Joa*_*uer 11
您可以使用零长度正向预测来分别指定每个约束:
(?=.{8,})(?=.*\p{Lu}.*\p{Lu})(?=.*[!@#$&*])(?=.*[0-9])(?=.*\p{Ll}.*\p{Ll})
Run Code Online (Sandbox Code Playgroud)
如果您正则表达式引擎不支持的\p符号和纯ASCII就够了,那么你就可以替换\p{Lu}使用[A-Z],并\p{Ll}用[a-z].
Jun*_*tri 11
不幸的是,上述所有正则表达式都不适合我。强密码的基本规则是
所以,最好的正则表达式是
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*]).{8,}$
Run Code Online (Sandbox Code Playgroud)
上述正则表达式的最小长度为 8。您可以将其从 {8,} 更改为 { any_number ,}
规则修改?
假设您想要最少x 个字符小写字母,y 个字符大写字母,z个字符数字,总最小长度w。然后尝试下面的正则表达式
^(?=.*[a-z]{x,})(?=.*[A-Z]{y,})(?=.*[0-9]{z,})(?=.*[!@#\$%\^&\*]).{w,}$
Run Code Online (Sandbox Code Playgroud)
注意:更改正则表达式中的x , y , z , w
编辑:更新了正则表达式答案
编辑2:添加修改
您还应该考虑将一些规则更改为:
通过上述改进,并且为了获得更大的灵活性和可读性,我将正则表达式修改为。
^(?=(.*[a-z]){3,})(?=(.*[A-Z]){2,})(?=(.*[0-9]){2,})(?=(.*[!@#$%^&*()\-__+.]){1,}).{8,}$
Run Code Online (Sandbox Code Playgroud)
基本说明
(?=(.*RULE){MIN_OCCURANCES,})
Run Code Online (Sandbox Code Playgroud)
每个规则块由 (?=(){}) 显示。然后可以轻松地指定规则和出现次数,并在组合之前单独测试
详细说明
^ start anchor
(?=(.*[a-z]){3,}) lowercase letters. {3,} indicates that you want 3 of this group
(?=(.*[A-Z]){2,}) uppercase letters. {2,} indicates that you want 2 of this group
(?=(.*[0-9]){2,}) numbers. {2,} indicates that you want 2 of this group
(?=(.*[!@#$%^&*()\-__+.]){1,}) all the special characters in the [] fields. The ones used by regex are escaped by using the \ or the character itself. {1,} is redundant, but good practice, in case you change that to more than 1 in the future. Also keeps all the groups consistent
{8,} indicates that you want 8 or more
$ end anchor
Run Code Online (Sandbox Code Playgroud)
最后,出于测试目的,这里是一个带有上述正则表达式的robulink
上面给出的答案是完美的,但我建议使用多个较小的正则表达式而不是大的正则表达式.
拆分长正则表达式有一些优点:
通常,这种方法可以使代码易于维护.
话虽如此,我分享了一段我在Swift中编写的代码示例:
struct RegExp {
/**
Check password complexity
- parameter password: password to test
- parameter length: password min length
- parameter patternsToEscape: patterns that password must not contains
- parameter caseSensitivty: specify if password must conforms case sensitivity or not
- parameter numericDigits: specify if password must conforms contains numeric digits or not
- returns: boolean that describes if password is valid or not
*/
static func checkPasswordComplexity(password password: String, length: Int, patternsToEscape: [String], caseSensitivty: Bool, numericDigits: Bool) -> Bool {
if (password.length < length) {
return false
}
if caseSensitivty {
let hasUpperCase = RegExp.matchesForRegexInText("[A-Z]", text: password).count > 0
if !hasUpperCase {
return false
}
let hasLowerCase = RegExp.matchesForRegexInText("[a-z]", text: password).count > 0
if !hasLowerCase {
return false
}
}
if numericDigits {
let hasNumbers = RegExp.matchesForRegexInText("\\d", text: password).count > 0
if !hasNumbers {
return false
}
}
if patternsToEscape.count > 0 {
let passwordLowerCase = password.lowercaseString
for pattern in patternsToEscape {
let hasMatchesWithPattern = RegExp.matchesForRegexInText(pattern, text: passwordLowerCase).count > 0
if hasMatchesWithPattern {
return false
}
}
}
return true
}
static func matchesForRegexInText(regex: String, text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matchesInString(text,
options: [], range: NSMakeRange(0, nsString.length))
return results.map { nsString.substringWithRange($0.range)}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我建议添加
(?!.*pass|.*word|.*1234|.*qwer|.*asdf) exclude common passwords
Run Code Online (Sandbox Code Playgroud)