學(xué)python的re模板,寫(xiě)了個(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ū)分大小寫(xiě),希望有興趣的可以自己寫(xiě)寫(xiě).問(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<'''
以上所述是小編給大家介紹的使用正則表達(dá)式判斷密碼強(qiáng)弱的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!
|
新聞熱點(diǎn)
疑難解答
圖片精選