展开全部
#encoding=utf-8
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):
#判断密码长度是32313133353236313431303231363533e58685e5aeb931333431346333否合法
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()
纯代码写就这样,还需要修改一下,业务中一般用正则表示:# -*- coding: utf8 -*-
import re
while True:
line = raw_input("input:")
if len(line) == 0:
print "NO"
else:
if re.search('^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{8,}$', line):
print 'YES'
else:
print "NO"