用于密码验证的python regex

2024-06-16 16:57:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

下面的程序正在进行部分匹配,但不是完整的正则表达式

#!/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"

使用中:

^{pr2}$

它正在评估错误的输出。有人能建议我在这里使用re.search?在


Tags: import程序re密码inputrawbinusr
1条回答
网友
1楼 · 发布于 2024-06-16 16:57:15

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

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

希望这对你有帮助。。。在

相关问题 更多 >