zoukankan      html  css  js  c++  java
  • python正则检测密码合法性

    客户系统升级,要求用户密码符合一定的规则,即:包含大小写字母、数字、符号,长度不小于8,于是先用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()

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

  • 相关阅读:
    csps模拟测试70
    模拟测试66反思
    csps63总结
    csps60爆零记
    csps模拟测试57
    模拟测试52,53反思
    LOJ2542「PKUWC2018」随机游走
    LOJ6300 BZOJ5283 [CodePlus 2018 3 月赛]博弈论与概率统计
    2019暑假集训
    省队二轮集训笔记
  • 原文地址:https://www.cnblogs.com/GarfieldTom/p/3780152.html
Copyright © 2011-2022 走看看