(!@#$&*)
(0-9)
有人可以给我等级。所有条件都必须通过密码满足。
您可以使用正面的主张进行这些检查:
^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
解释:
(?=(.*RULE){MIN_OCCURANCES,})
EAVER规则块由(?=(){})显示。然后,可以轻松地指定和分别测试出事件的规则和数量,然后再进行组合
^ 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
漏,上面的正则是
上面给出的答案是完美的,但是我使用多个较小的正则义务而不是大的正则。 分裂长时间的正则有一些优势:
撰写和阅读的时尚 depug的时尚
添加/删除正则一部分的气息
从基础上讲,此方法保持代码可维护
。having说,我在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 []
}
}
}
至少包含大写字母
至少包含一个小字母
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*]).{8,}$
规则中的修改?
llet'说您想要最小的x字符小字母,
z
字符号,总长度^(?=.*[a-z]{x,})(?=.*[A-Z]{y,})(?=.*[0-9]{z,})(?=.*[!@#\$%\^&\*]).{w,}$
,
wEdit:更新的正则答案
Edit2:添加了修改
您可以使用零长度的阳性look-preads分别指定您的每个约束:
(?=.{8,})(?=.*\p{Lu}.*\p{Lu})(?=.*[!@#$&*])(?=.*[0-9])(?=.*\p{Ll}.*\p{Ll})
如果您的正则发动机不支持\p
符号,而纯ASCII就足够了,那么您可以用\p{Lu}
和[A-Z]
代替\p{Ll}
。
我建议添加
[a-z]
codaddict的解决方案效果很好,但是这个解决方案效率更高:(Python语法)
(?!.*pass|.*word|.*1234|.*qwer|.*asdf) exclude common passwords
被否定的字符类在一个步骤中消耗到所需字符的所有内容,需要回溯为零。 (点星解决方案效果很好,但确实需要一些回溯。)当然,对于诸如密码之类的简短目标字符串,这种效率提高将可以忽略不计。
password = re.compile(r"""(?#!py password Rev:20160831_2100)
# Validate password: 2 upper, 1 special, 2 digit, 1 lower, 8 chars.
^ # Anchor to start of string.
(?=(?:[^A-Z]*[A-Z]){2}) # At least two uppercase.
(?=[^!@#$&*]*[!@#$&*]) # At least one "special".
(?=(?:[^0-9]*[0-9]){2}) # At least two digit.
.{8,} # Password length is 8 or more.
$ # Anchor to end of string.
""", re.VERBOSE)
其他解决方案:
import re
RegexLength=re.compile(r'^\S{8,}$')
RegexDigit=re.compile(r'\d')
RegexLower=re.compile(r'[a-z]')
RegexUpper=re.compile(r'[A-Z]')
def IsStrongPW(password):
if RegexLength.search(password) == None or RegexDigit.search(password) == None or RegexUpper.search(password) == None or RegexLower.search(password) == None:
return False
else:
return True
while True:
userpw=input("please input your passord to check: \n")
if userpw == "exit":
break
else:
print(IsStrongPW(userpw))
codaddict给出了一个很好的答案:
import re
passwordRegex = re.compile(r'''(
^(?=.*[A-Z].*[A-Z]) # at least two capital letters
(?=.*[!@#$&*]) # at least one of these special c-er
(?=.*[0-9].*[0-9]) # at least two numeric digits
(?=.*[a-z].*[a-z].*[a-z]) # at least three lower case letters
.{8,} # at least 8 total digits
$
)''', re.VERBOSE)
def userInputPasswordCheck():
print('Enter a potential password:')
while True:
m = input()
mo = passwordRegex.search(m)
if (not mo):
print('''
Your password should have at least one special charachter,
two digits, two uppercase and three lowercase charachter. Length: 8+ ch-ers.
Enter another password:''')
else:
print('Password is strong')
return
userInputPasswordCheck()
但是,如果您想拥有,请说8个小写字母而不是3个字母,写下类似的东西
^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
真的很麻烦,所以你可以写(?=.*[a-z].*[a-z].*[a-z].*[a-z].*[a-z].*[a-z].*[a-z].*[a-z])
(?=(?:.*[a-z]){8})
用于排除捕获组
我认为更好的答案是:
注意,
^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=(?:.*[a-z]){3}).{8}$
会有所不同,因为它是连续8个小写字母的正则罚款。
(?=.*[a-z]{8})
@thsks @ridgerunner
[至少1个大写字符(A-Z) 至少1个小写字符(A-Z) 至少1位数字(0-9) 至少有1个特殊角色 - 不要忘记将空间视为特殊角色]
至少10个字符
大多数128个字符
无需连续2个以上相同的字符(例如,不允许111个字符)
'^(?!。(。){2}) ((?=。
[a-z])(?=。
[a-z])(?=。[0-9])|(?=。
[a-z])(?=。=。[a-z])( ?=。[^a-za-Z0-9])|(?=。
[a-z])(?=。
[0-9])(?=。= ])|(?=。[a-z])(?=。[0-9])(?=。*[^a-Za-Z0-9]))
(?!。*(。){2})
(?=。[a-z])(?=。[a-z])(?=。
[a-z])(?=。*[^a-Za-Z0-9])
(?=。[A-Z])(?=。
(?=。
[A-Z])(?=。[0-9])(?=。*[^a-Za-Z0-9])
。{10.127}
if(preg_match("/^(?=(?:[^A-Z]*[A-Z]){2})(?=(?:[^0-9]*[0-9]){2}).{8,}$/",
'CaSu4Li8')){
return true;
}else{
return fasle;
}