本文给大家分享使用正则表达式来判断密码强弱的实例代码,非常不错,具有参考借鉴价值,需要的朋友参考下吧
学python的re模板,写了个文章发现没人看,所以总结出来经验,理论没人爱,实战的人心,那么既然没人喜欢理论就直接上实战,在实战中精炼理论.不多说直接先上代码
def password_level(password):
weak = re.compile(r'^((\d+)|([A-Za-z]+)|(\W+))$')
level_weak = weak.match(password)
level_middle = re.match(r'([0-9]+(\W+|\_+|[A-Za-z]+))+|([A-Za-z]+(\W+|\_+|\d+))+|((\W+|\_+)+(\d+|\w+))+',password)
level_strong = re.match(r'(\w+|\W+)+',password)
if level_weak:
print 'password level is weak',level_weak.group()
else:
if (level_middle and len(level_middle.group())==len(password)):
print 'password level is middle',level_middle.group()
else:
if level_strong and len(level_strong.group())==len(password):
print 'password level is strong',level_strong.group()解释一下
弱密码:全是数字,符号,字母
中等密码:数字加上符号,数字加上字母,字母加上符号
强密码:三个混合.
我没有区分大小写,希望有兴趣的可以自己写写.问题出现在\w上因为\w等价与[A-Za-z0-9_]所以前期通过\W不能匹配到包含下滑线的字符串
我们来看看中等密码,数字加上符号或者字母或者_是一个组,字母加上符号或者下划线或者符号是一个组,符号或者下划线加上字母或者数字是一个组,我总觉得这个里面的代码好像不对但是通过测试又没发现什么不对的地方,就先用这个版本0.0.1吧
测试代码
if name == 'main':
passwords = ('11','aa','LL','1a','1_','a_','a1','_1','*a','1a_','1a<')
for pw in passwords:
password_level(pw)
'''----------------------output------------------------
#password level is weak 11
#password level is weak aa
#password level is weak LL
#password level is middle 1a
#password level is middle 1_
#password level is middle a_
#password level is middle a1
#password level is middle _1
#password level is middle *a
#password level is strong 1a_
#password level is strong 1a<
'''以上就是分享一个用正则表达式判断密码强弱的实例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号