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实现的将文件每一列写入列表功能示例【测试可用】

Python实现的将文件每一列写入列表功能示例【测试可用】

本文实例讲述了Python实现的将文件每一列写入列表功能。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- #! python3 ''' python...

Python3进制之间的转换代码实例

Python3进制之间的转换 在Python里面实现进制之间的转换是非常方便的,有专门的函数来进行这个操作: 下面直接上代码: # 进制之间的转换 a = 10 # 转换为二进制...

Python中二维列表如何获取子区域元素的组成

Python中二维列表如何获取子区域元素的组成

用过NumPY的应该都知道,在二维数组中可以方便地使用区域切片功能,如下图: 而这个功能在Python标准库的List中是不支持的,在List中只能以一维方式来进行切片操作: 但有时...

浅析Python 实现一个自动化翻译和替换的工具

浅析Python 实现一个自动化翻译和替换的工具

一个可能你似曾相识的场景 阅读内容包含大量英文的 PPT、Word、Excel 或者记事本时,由于英语不熟悉,为了流利地阅读,需要打开浏览器进入谷歌翻译的主界面,然后把英文复制到谷歌翻译...

详解Python self 参数

1、概述 1.1 场景 我们在使用 Python 中的 方法 method 时,经常会看到 参数中带有 self,但是我们也没对这个参数进行赋值,那么这个参数到底是啥意思呢? 2、知识点...