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

yipeiwu_com7年前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脚本成为Windows中运行的exe文件

将程序转换为exe文件 我们先来介绍如何使用工具Pyinstaller 安装Pyinstaller 我们用pip安装Pyinstaller 。 注意,如果使用Pyinstaller,则应...

TensorFlow 合并/连接数组的方法

如下所示: import tensorflow as tf a = tf.Variable([4,5,6]) b = tf.Variable([1,2,3]) c = tf.co...

python中argparse模块用法实例详解

本文实例讲述了python中argparse模块用法。分享给大家供大家参考。具体分析如下: 平常在写命令行工具的时候,经常会带参数,所以用python中的argparse来实现。 #...

Python实现栈的方法

本文实例讲述了Python实现栈的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python #定义一个列表来模拟栈 stack = [] #进...

详细解读Python的web.py框架下的application.py模块

本文主要分析的是web.py库的application.py这个模块中的代码。总的来说,这个模块主要实现了WSGI兼容的接口,以便应用程序能够被WSGI应用服务器调用。WSGI是Web...