Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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])(?=.*[@#$])[wd@#$]{6,12}$", password):
    print ("match")
else:
    print ("Not Match")

Hope this will Help You...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...