检查密码时出现正则表达式错误

2024-06-28 11:03:19 发布

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

在javascript中,我试图检查一个密码必须至少有8个字符,并且必须至少包含一个字母、一个数字以及除_@.- . 为此,我使用这个正则表达式

^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}

我想用一根绳子来匹配它

^{pr2}$

它给出syntex错误

SyntaxError: expected expression, got '^'

我还要在python中检查同样的东西。在


Tags: 密码错误字母数字javascriptexpectedexpressiongot
2条回答

如果要在不使用regex的Python中检查这些条件:

def check_password(password):
    return len(password) > 7 and any(character.isalpha() for character in password) and any(character.isdigit() for character in password) and all(character.isalnum() or character in '_@.-' for character in password)

或者:

^{pr2}$

JS正则表达式文本需要包装在/../

/^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}/.test('password')

相关问题 更多 >