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

yipeiwu_com6年前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读取word 中指定位置的表格及表格数据

python读取word 中指定位置的表格及表格数据

1.Word文档如下: 2.代码 # -*- coding: UTF-8 -*- from docx import Document def readSpecTable(filen...

Python3使用requests登录人人影视网站的方法

早就听说requests的库的强大,只是还没有接触,今天接触了一下,发现以前使用urllib,urllib2等方法真是太搓了…… 这里写些简单的使用初步作为一个记录 本文继续练习使用re...

python多线程共享变量的使用和效率方法

python多线程可以使任务得到并发执行,但是有时候在执行多次任务的时候,变量出现“意外”。 import threading,time n=0 start=time.time()...

Python深入学习之特殊方法与多范式

Python一切皆对象,但同时,Python还是一个多范式语言(multi-paradigm),你不仅可以使用面向对象的方式来编写程序,还可以用面向过程的方式来编写相同功能的程序(还有函...

Python上下文管理器用法及实例解析

这篇文章主要介绍了Python上下文管理器用法及实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 with上下文管理器 语法:...