用于密码验证的python正则表达式

问题描述:

我有以下要求来验证以下上下文的密码

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

  • 至少一位数
  • 至少一个大写字母
  • 至少一个小写字母
  • 至少一个特殊字符[$@#]

下面的程序是做部分匹配,而不是完整的正则表达式

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"

正在使用中:

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

它正在评估错误的输出.任何人都可以在这里建议我应该使用 re.search 吗?

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

这里是至少一位数字、一位大写字母、至少一位小写字母、至少一位特殊字符的正则表达式

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")

希望这会帮助你...