菜鳥使用python實現(xiàn)正則檢測密碼合法性
客戶系統(tǒng)升級,要求用戶密碼符合一定的規(guī)則,即:包含大小寫字母、數字、符號,長度不小于8,于是先用python寫了個簡單的測試程序:
在寫解決方案前,列一下
python正則表達式中的特殊字符:
^ 表示匹配的字符必須在最前邊
$ 表示匹配的字符必須在最后邊
* 匹配* 前面的字符0次或n次
+ 匹配+ 前面的字符1次或n次
? 匹配?前面的字符0次或1次
. (小數點)匹配除換行符外的所有字符
(x) 匹配x并記錄匹配的值
x|y 匹配x或者y
{n} 這里n是一個正整數。匹配前面的n個字符
{n,} 這里n是一個正整數。匹配至少n個前面的字符
{n,m} 這里n和m都是正整數。匹配至少n個、最多m個前面的字符
[xyz] 字符列表,匹配表中的任一字符,可以通過連接字符 - 指出字符范圍,如 [a-z] 表示所有小寫字符
[b] 匹配一個空格
b 匹配一個單詞的分界線,比如一個空格
B 匹配一個單詞的非分界線
re模塊匹配規(guī)則(re.match函數的第三個參數):
re.IGNORECASE 忽略文中的大小寫
re.LOCALE 處理字符集本地化
re.MULTILINE 是否支持多行匹配
re.DOTALL 匹配一些特殊標記,例如使用.匹配\n等字符
re.VERBOSE 忽略正則表達式中的空格或者換行等字符
re.UNICODE 使用Unicode編碼
#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()
平時用正則不多,不知道怎么寫一個正則滿足要求,用了比較笨的辦法,誰知道一句正則檢驗的請賜教!
我們再來看一個稍微復雜些的 檢測郵箱名及密碼驗證
代碼:
main.py
# coding=gbk import re def ProcessMail(inputMail): isMatch = bool(re.match(r"^[a-zA-Z](([a-zA-Z0-9]*\.[a-zA-Z0-9]*)|[a-zA-Z0-9]*)[a-zA-Z]@([a-z0-9A-Z]+\.)+[a-zA-Z]{2,}$", inputMail,re.VERBOSE)); if isMatch: print ("郵箱注冊成功。"); else: print ("郵箱注冊失敗。"); return isMatch; def ProcessPassword(inputPassword): #處理正則表達式 isMatch = bool(re.match(r"[a-zA-Z0-9]{8}",inputPassword,flags=0)); #用type的三位表示數字type[0],小寫字母type[1],大寫字母type[2]是否都具備 if isMatch: type = [False,False,False] for i in range(0,8): temp = inputPassword[i] if ord(temp) >= ord('0') and ord(temp) <= ord('9'): type[0] = True; elif ord(temp) >= ord('a') and ord(temp) <= ord('z'): type[1] = True; elif ord(temp) >= ord('A') and ord(temp) <= ord('Z'): type[2] = True; for i in type: if i is False: isMatch = False; break; #處理是否有重復的字符出現(xiàn) if isMatch: for i in range(0,7): temp = inputPassword[i]; for j in range(i + 1,8): if inputPassword[j] == temp: isMatch = False; break; if isMatch: print ("密碼注冊成功。"); else: print ("密碼注冊失敗。"); return isMatch; if __name__ == '__main__': mailState = False; while mailState is False: inputMail = input("輸入郵箱: "); mailState = ProcessMail(inputMail); print ("\n"); # passwordState = False; while passwordState is False: inputPassword = input("輸入密碼: "); passwordState = ProcessPassword(inputPassword); print ("\n");
輸出:
輸入郵箱: a.a9@sina.com 郵箱注冊失敗。 輸入郵箱: 9a.aa@sina.com 郵箱注冊失敗。 輸入郵箱: a.a.a@sina.com 郵箱注冊失敗。 輸入郵箱: a9999@sina.com 郵箱注冊失敗。 輸入郵箱: a123.banana@..com 郵箱注冊失敗。 輸入郵箱: a123.banana@a..com 郵箱注冊失敗。 輸入郵箱: a123.banana@sina.c 郵箱注冊失敗。 輸入郵箱: a123.banana@sina.com 郵箱注冊失敗。 輸入郵箱: a123.banana@sina.com 郵箱注冊成功。
密碼的測試也滿足需求,不一一列舉
相關文章
python協(xié)程之yield和yield?from實例詳解
Python在并發(fā)處理上不僅提供了多進程和多線程的處理,還包括了協(xié)程,下面這篇文章主要給大家介紹了關于python協(xié)程之yield和yield?from的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12python list count統(tǒng)計個數的實現(xiàn)
這篇文章主要介紹了python list count統(tǒng)計個數的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02