Python编程快速上手 7.18.1 强口令检测
写一个函数,它使用正则表达式,确保传入的口令字符串是强口令。强口令的定义是:长度不少于8个字符,同事包含大写和小写字符,至少有一个数字。你可能需要用多个正则表达式来测试该字符串,以确保它的强度。
以下为我想到的方法,肯定有更好的,但我暂时不知道,有其他方法的欢迎一起评论交流呀~
#! python3
# 长度不少于8个字符,同时包含大小写,至少有一位数字。
import re
theRegex1 = re.compile(r'[a-zA-Z0-9]{8,}')
theRegex2 = re.compile(r'[A-Z]+')
theRegex3 = re.compile(r'[a-z]+')
theRegex4 = re.compile(r'[0-9]+')
text = '0aaa.Aaaa'
print(theRegex1.search(text) != None) #长度8以上
if theRegex1.search(text) != None:
print('长度8以上')
if theRegex2.search(text) != None:
print('有大写')
if theRegex3.search(text) != None:
print('有小写')
if theRegex4.search(text) != None:
print('有数字')
print(theRegex1.search(text))
else:
print('没数字')
else:
print('没小写')
else:
print('没大写')
else:
print('长度不够8,或者有其他字符')