I want to check if a password contains special characters. I have googled for a few examples but cant find that addresses my problem. How do I do it? Here is how I am trying it so far;
elif not re.match("^[~!@#$%^&*()_+{}":;']+$",password)
print "Invalid entry."
continue
My string is password.
You don't need a regex for this. Try:
elif set('[~!@#$%^&*()_+{}":;\']+$').intersection(password):
print "Invalid entry."
The quotation mark has been escaped in the string. What this does is create a set containing all your invalid characters, then take the intersection of it and password
(in other words, a set containing all unique characters that exist in both the set and the password string). If there are no matches, the resulting set will be empty, and thus evaluate as False
, otherwise it will evaluate as True
and print the message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With