简体   繁体   English

让密码检查器识别 Python 中的特殊字符

[英]Getting a password checker to recognize special characters in Python

I'm struggling to get my password checker to recognize my special characters as true when they are inputted, any help would be appreciated!我正在努力让我的密码检查器在输入时将我的特殊字符识别为真,我们将不胜感激!

import re

def password_cri():
    while True:
        password = input("Please enter your password:")
        if len(password)<5 and len(password)>15:
            print("Password denied: must be between 5 and 15 characters long.")
        elif re.search('[0-9]',password) is None:
            print("Password denied: must contain a number between 0 and 9")
        elif re.search('[A-Z]',password) is None:
            print("Password denied: must contain a capital letter.")
        elif re.search('[a-z]',password) is None:
            print("Password denied: must contain a lowercase letter.")
        elif re.search('[!, @, #, $, %, &, (, ), -, _, [, ], {, }, ;, :, ", ., /, <, >, ?]', password) is None:
            print("Password denied: must contain a special character")          
        else:
            print("Your password has been accepted.")
            break    

password_cri()
'[!, @, #, $, %, &, (, ), -, _, [, ], {, }, ;, :, ", ., /, <, >, ?]'

is probably not the regex you're looking for.可能不是您要查找的正则表达式。 Try尝试

'[!@#$%&()\-_[\]{};:"./<>?]'

and note that - and ] are escaped because of how the [] regex block works.并注意-]由于[]正则表达式块的工作方式而被转义。

That's not the proper regular expression for matching those characters.这不是匹配这些字符的正确正则表达式。

I wouldn't actually recommend using a regular expression for this, since your matches are only one character long.我实际上不建议为此使用正则表达式,因为您的匹配项只有一个字符长。 Instead, make a string containing all the special characters you want to match, and then use any() and map() to determine if any of the special characters appear in the password.相反,创建一个包含所有要匹配的特殊字符的字符串,然后使用any()map()来确定密码中是否出现任何特殊字符。

password = "????????"
special_characters = '!@#$%&()-_[]{};:"./<>?'

if any(map(lambda x: x in password, special_characters)):
    print("Password contains special characters")
else:
    print("Password denied: must contain a special character")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM