如何修复python中的(regex password)错误?

2021-05-16 08:08:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在设置我的服务器,我想有复杂的密码。你知道吗

在Python中使用regex,如何验证用户的密码是:

至少8个字符

必须限于,但不特别要求:

大写字母:A-Z

小写字母:a-z

数字:0-9

任何特殊字符:@#$%^&;+=

import re

password = raw_input("Enter string to test: ")

if re.match(r'[A-Za-z0-9@#$%^&+=]{8,}', password):

    # match
else:

    # no match

from wtforms import StringField, PasswordField, SubmitField, BooleanField

from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError


class RegistrationForm(FlaskForm):

    username = StringField('Username',
                           validators=[DataRequired(), Length(min=2, max=20)])
    email = StringField('Email',
                        validators=[DataRequired(), Email()])
    #password = 
    pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$"
    password = PasswordField('Password', validators=[DataRequired(), Length(min=2, max=20)])
    if re.match(r'[A-Za-z0-9@#$%^&+=]{8,}', password):
        print ("Valid password")
    else:
        print ("Password not valid")

我希望输出是有效或无效的密码