python3 用正则判断输入的密码是否符合要求

python3 用正则判断输入的密码是否符合要求

下面代码接受输入一串字符作为密码,并用正则判断密码是否符合要求:1.必须大于或等于8个字符;2.必须同时包含大小写字母和数字;3.不接受除字母和数字以外的字符。不符合要求时打印相应的提示。

#! python3
# coding=utf-8

import re

lowerRegex = re.compile('[a-z]')
upperRegex = re.compile('[A-Z]')
digitRegex = re.compile('[0-9]')
wrongRegex = re.compile('[^a-zA-Z0-9]')

while True:
    password = input('请输入大于8位的包含大小写字母和数字的密码:')
    if len(password) < 8:
        print('输入的密码小于8位')
    elif wrongRegex.search(password) != None:
        print('包含无效字符')
    else:
        if lowerRegex.search(password) == None:
            print('未包含小写字母')
        elif upperRegex.search(password) == None:
            print('未包含大写字母')
        elif digitRegex.search(password) == None:
            print('未包含数字')
        else:
            print('输入成功')
            break

猜你喜欢

转载自blog.csdn.net/xumoumou233/article/details/82559136
今日推荐