我在python中有这个代码。 实际上我想使用python在服务器端验证密码字段。我希望密码本质上是字母数字,这就是我到目前为止所做的事情
def valid():
num = "[a-zA-Z0-9]"
pwd = "asdf67AA"
match = re.match(pwd, num)
if match:
return True
else:
return False
它总是返回假。 没有达到IF条件。什么是我失踪
答案 0 :(得分:2)
你颠倒了匹配的参数
re.match(pattern, string, flags=0)
应该是
match = re.match(num, pwd)
答案 1 :(得分:1)
您可以执行此操作以查明字符串是否为字母数字
pwd = "asdf67AA"
if pwd.isalnum():
# password is alpha numeric
else:
# password is not alpha numeric
答案 2 :(得分:0)
你错过了将方法放在方法中反转它。
它会起作用。
但如果在字符串中设置符号,这也会匹配。 所以,我建议你应用这些东西:
pwd =“test5456”
pwd.isalnum():
此方法仅适用于字符串包含字符串或数字的情况。
如果您强烈希望您的密码包含数字和字符串,则此代码将对您有所帮助。
test = "anil13@@"
new = tuple(test)
data = list(new)
is_symb =False
is_digit = False
is_alpha = False
for d in data :
if d.isdigit() :
is_digit = True
if d.isalpha():
is_alpha = True
if not d.isdigit() and not d.isalpha():
is_symb =True
break
if is_symb == False and is_digit == True and is_alpha == True:
print "pwd matchd::::"
else :
print "pwd dosen't match..."
注意:此代码非常适用于数字&符号
此致 阿尼尔
答案 3 :(得分:0)
这是一个非常简单的答案,最好使用正则表达式进行验证,而不是使用有线if if条件。
import re
password = raw_input("Enter Your password: ")
if re.match(r'[A-Za-z0-9@#$%^&+=]{6,}', password):
print "Password is match...."
else:
print "Password is not match...."
#6 means length of password must be atleast 6
答案 4 :(得分:-1)
你可以使用python的isalnum()方法来实现 只是做着
pwd.isalnum()
它会告诉一个数字是否是字母数字,然后会点击if condiion