What is wrong with the below used regular expression? Why does it not match the password?
import re
pattern = re.compile ('^\w@#$%{8,}')
password = '12345abcd@#$%'
x = pattern.search(password)
print (x)
print (len(password))
CodePudding user response:
You didn't escape the $
which has a special meaning in a regular expression and didn't put the allowed characters in square brackets to allow any of them.
This: ^[\w@#\$%]{8,}
is the modified version of the regex which matches the password.
Escaping the $ character isn't really necessary within square brackets so ^[\w@#$%]{8,}
will work as well.
I suggest you check your regular expressions here: https://regex101.com/r/ldvJLf/1 . This site explains in detail the meaning of all single elements of the regular expression, so you can directly see what is wrong if things doesn't work as you expected.
CodePudding user response:
Tip:
check your regexes online https://regexr.com/
I think you want:
pattern = re.compile ('^[\w@#$%]{8,}')