學(xué)python的re模板,寫了個(gè)文章發(fā)現(xiàn)沒(méi)人看膊升,所以總結(jié)出來(lái)經(jīng)驗(yàn)怎炊,理論沒(méi)人愛(ài),實(shí)戰(zhàn)的人心廓译,那么既然沒(méi)人喜歡理論就直接上實(shí)戰(zhàn)评肆,在實(shí)戰(zhàn)中精煉理論.不多說(shuō)直接先上代碼
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()
解釋一下
弱密碼:全是數(shù)字,符號(hào)非区,字母
中等密碼:數(shù)字加上符號(hào)瓜挽,數(shù)字加上字母,字母加上符號(hào)
強(qiáng)密碼:三個(gè)混合.
我沒(méi)有區(qū)分大小寫征绸,希望有興趣的可以自己寫寫.問(wèn)題出現(xiàn)在\w上因?yàn)閈w等價(jià)與[A-Za-z0-9_]所以前期通過(guò)\W不能匹配到包含下滑線的字符串
我們來(lái)看看中等密碼久橙,數(shù)字加上符號(hào)或者字母或者_是一個(gè)組,字母加上符號(hào)或者下劃線或者符號(hào)是一個(gè)組歹垫,符號(hào)或者下劃線加上字母或者數(shù)字是一個(gè)組剥汤,我總覺(jué)得這個(gè)里面的代碼好像不對(duì)但是通過(guò)測(cè)試又沒(méi)發(fā)現(xiàn)什么不對(duì)的地方,就先用這個(gè)版本0.0.1吧
測(cè)試代碼
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<
'''
每天沉迷python排惨,壞狗狗你在哪里