Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regex for password validation

I have the following requirement to validate the password with below context

  • at least one digit
  • at least one uppercase letter
  • at least one lowercase letter
  • at least one special character[$@#]

Below program is doing the partial match but not the full regex

#!/usr/sfw/bin/python
import re
password = raw_input("Enter string to test: ")
if re.match(r'[A-Za-z0-9@#$]{6,12}', password):
    print "match"
else:
    print "Not Match"

In use:

localhost@user1$ ./pass.py
Enter string to test: abcdabcd
match

It is evaluating the wrong output. Can anyone suggest here should I use re.search?

like image 487
user216358 Avatar asked Nov 20 '25 09:11

user216358


1 Answers

Here is the Regex for at least one digit, one uppercase letter, at least one lowercase letter, at least one special character

import re
password = input("Enter string to test: ")
# Add any special characters as your wish I used only #@$
if re.match(r"^(?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[@#$])[\w\d@#$]{6,12}$", password):
    print ("match")
else:
    print ("Not Match")

Hope this will Help You...

like image 167
Muthu Kumar Avatar answered Nov 22 '25 23:11

Muthu Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!