在 Python 中使用正则表达式,我如何验证用户的密码是:
Using a regex in Python, how can I verify that a user's password is:
至少 8 个字符必须限于,但不具体要求:大写字母:A-Z小写字母:a-z数字:0-9任何特殊字符:@#$%^&+=注意,所有字母/数字/特殊字符都是可选的.我只想验证密码长度是否至少为 8 个字符,并且仅限于字母/数字/特殊字符.如果他们愿意,用户可以选择更强/更弱的密码.到目前为止,我所拥有的是:
Note, all the letter/number/special chars are optional. I only want to verify that the password is at least 8 chars in length and is restricted to a letter/number/special char. It's up to the user to pick a stronger / weaker password if they so choose. So far what I have is:
import re
pattern = "^.*(?=.{8,})(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$"
password = raw_input("Enter string to test: ")
result = re.findall(pattern, password)
if (result):
print "Valid password"
else:
print "Password not valid"
import re
password = raw_input("Enter string to test: ")
if re.fullmatch(r'[A-Za-z0-9@#$%^&+=]{8,}', password):
# match
else:
# no match
{8,}
表示至少 8 个"..fullmatch
函数要求整个字符串匹配整个正则表达式,而不仅仅是一部分.
The {8,}
means "at least 8". The .fullmatch
function requires the entire string to match the entire regex, not just a portion.
上一篇:如何隐藏通过 JavaScript 对话框提示输入的密码?对话框、提示、密码、JavaScript
下一篇:如何存储在共享preference ArrayList的价值观?价值观、preference、ArrayList