python使用正則表達式檢測密碼強度源碼分享
更新時間:2014年06月11日 10:03:49 作者:
客戶系統(tǒng)升級,要求用戶密碼符合一定的規(guī)則,即:包含大小寫字母、數字、符號,長度不小于8,于是先用python寫了個簡單的測試程序:
復制代碼 代碼如下:
#encoding=utf-8
#-------------------------------------------------------------------------------
# Name: 模塊1
# Purpose:
#
# Author: Administrator
#
# Created: 10-06-2014
# Copyright: (c) Administrator 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import re
def checklen(pwd):
return len(pwd)>=8
def checkContainUpper(pwd):
pattern = re.compile('[A-Z]+')
match = pattern.findall(pwd)
if match:
return True
else:
return False
def checkContainNum(pwd):
pattern = re.compile('[0-9]+')
match = pattern.findall(pwd)
if match:
return True
else:
return False
def checkContainLower(pwd):
pattern = re.compile('[a-z]+')
match = pattern.findall(pwd)
if match:
return True
else:
return False
def checkSymbol(pwd):
pattern = re.compile('([^a-z0-9A-Z])+')
match = pattern.findall(pwd)
if match:
return True
else:
return False
def checkPassword(pwd):
#判斷密碼長度是否合法
lenOK=checklen(pwd)
#判斷是否包含大寫字母
upperOK=checkContainUpper(pwd)
#判斷是否包含小寫字母
lowerOK=checkContainLower(pwd)
#判斷是否包含數字
numOK=checkContainNum(pwd)
#判斷是否包含符號
symbolOK=checkSymbol(pwd)
print(lenOK)
print(upperOK)
print(lowerOK)
print(numOK)
print(symbolOK)
return (lenOK and upperOK and lowerOK and numOK and symbolOK)
def main():
if checkPassword('Helloworld#123'):
print('檢測通過')
else:
print('檢測未通過')
if __name__ == '__main__':
main()
平時用正則不多,不知道怎么寫一個正則滿足要求,用了比較笨的辦法,誰知道一句正則檢驗的請賜教!
相關文章
Python使用matplotlib創(chuàng)建Gif動圖的思路
這篇文章主要介紹了Python使用matplotlib創(chuàng)建Gif動圖,我們將討論matplotlib提供的名為“Animation”的動畫庫之一,Python二維繪圖庫是Matplolib可以輕松創(chuàng)建繪圖、直方圖、條形圖、散點圖等,需要的朋友可以參考下2022-04-04