python使用正则表达式检测密码强度源码分享
程序员文章站
2023-11-26 11:01:22
复制代码 代码如下:#encoding=utf-8#------------------------------------------------------------...
复制代码 代码如下:
#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()
平时用正则不多,不知道怎么写一个正则满足要求,用了比较笨的办法,谁知道一句正则检验的请赐教!