在javascript中我试图检查密码必须至少8个字符长,并且必须包含至少一个字母,一个数字,除了_ @ .-之外没有特殊符号。 为此,我正在使用这个常规表达
^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}
但当我尝试将字符串与
匹配时^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}.test('password')
它给出了syntex错误
SyntaxError: expected expression, got '^'
我也要在python中检查同样的事情。
答案 0 :(得分:4)
JS正则表达式文字需要包含在/
.. /
:
/^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}/.test('password')
答案 1 :(得分:2)
如果你想在没有正则表达式的情况下在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)
可替换地:
def check_password(password):
if len(password) < 8: return False
alpha = digit = False
nosymbol = True
for character in password:
if character.isalpha():
alpha = True
if character.isdigit():
digit = True
if not (character.isalnum() or character in '_@.-'):
nosymbol = False
return alpha and digit and nosymbol