>  기사  >  백엔드 개발  >  python使用正则表达式检测密码强度源码分享

python使用正则表达式检测密码强度源码分享

WBOY
WBOY원래의
2016-06-06 11:31:131482검색

代码如下:


#encoding=utf-8
#-------------------------------------------------------------------------------
# Name:        模块1
# Purpose:
#
# Author:      Administrator
#
# Created:     10-06-2014
# Copyright:   (c) Administrator 2014
# 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()

平时用正则不多,不知道怎么写一个正则满足要求,用了比较笨的办法,谁知道一句正则检验的请赐教!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.