用于密码验证的python正则表达式 [英] python regex for password validation

查看:78
本文介绍了用于密码验证的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")

希望这会帮助你...

这篇关于用于密码验证的python正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆