學python的re模板,寫了個文章發(fā)現(xiàn)沒人看,所以總結出來經(jīng)驗,理論沒人愛,實戰(zhàn)的人心,那么既然沒人喜歡理論就直接上實戰(zhàn),在實戰(zhàn)中精煉理論.不多說直接先上代碼
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ù)字,符號,字母
中等密碼:數(shù)字加上符號,數(shù)字加上字母,字母加上符號
強密碼:三個混合.
我沒有區(qū)分大小寫,希望有興趣的可以自己寫寫.問題出現(xiàn)在/w上因為/w等價與[A-Za-z0-9_]所以前期通過/W不能匹配到包含下滑線的字符串
我們來看看中等密碼,數(shù)字加上符號或者字母或者_是一個組,字母加上符號或者下劃線或者符號是一個組,符號或者下劃線加上字母或者數(shù)字是一個組,我總覺得這個里面的代碼好像不對但是通過測試又沒發(fā)現(xiàn)什么不對的地方,就先用這個版本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<'''
以上所述是小編給大家介紹的使用正則表達式判斷密碼強弱的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!
|
新聞熱點
疑難解答