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
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值