知识点:正则表达式。
强密码检测
编写一个使用正则表达式的函数,以确保其传递的密码字符串很强。强密码被定义为至少八个字符长,包含大写和小写字符,并且至少有一个数字。您可能需要针对多个正则表达式模式测试字符串以验证其强度。
源代码:
#checkPassword.py 检测密码强度
import re
def checkLen(pwd):
return len(pwd)>=8
def checkContainUpper(pwd):
pattern = re.compile('[A-Z]+')
match = pattern.findall(pwd)
if match:
return True
else:
return False
def checkContainNum(pwd):
pattern = re.compile('[0-9]+')
match = pattern.findall(pwd)
if match:
return True
else:
return False
def checkContainLower(pwd):
pattern = re.compile('[a-z]+')
match = pattern.findall(pwd)
if match:
return True
else:
return False
def checkSymbol(pwd):
pattern = re.compile('(^[a-zA-Z0-9])+')
match = pattern.findall(pwd)
if match:
return True
else:
return False
def checkPassword(pwd):
#判断密码长度是否合法
lenOK=checkLen(pwd)
#判断是否包含大写字母
upperOK=checkContainUpper(pwd)
#判断是否包含小写字母
lowerOK=checkContainLower(pwd)
#判断是否包含数字
numOK=checkContainNum(pwd)
#判断是否包含符号
symbolOK=checkSymbol(pwd)
print(lenOK)
print(upperOK)
print(lowerOK)
print(numOK)
print(symbolOK)
return (lenOK and upperOK and lowerOK and numOK and symbolOK)
def main():
if checkPassword('Helloworld#123'):
print('检测通过')
else:
print('检测未通过')
if __name__ == '__main__':
main()
输出:
True
True
True
True
True
检测通过