欢迎来到代码驿站!

当前位置:首页 >

使用正则表达式判断密码强弱

时间:2021-04-14 09:03:02|栏目:|点击:

学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<
'''

上一篇:Mac下部署springBoot项目到Docker中(demo)

栏    目:

下一篇:易语言打开文件件网页的方法

本文标题:使用正则表达式判断密码强弱

本文地址:http://www.codeinn.net/misctech/100787.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有