Skip to main content
 首页 » 编程设计

python之正则表达式 Python 模式

2025年04月02日36oomusou

我正在尝试编写一个程序,该程序使用正则表达式来确保传递给它的密码字符串是强密码。强密码定义为至少八个字符长、包含大写和小写字符并且至少有一位数字的密码。到目前为止,我有以下代码。我花了几个小时修改正则表达式,但我无法让它通过。每次我进行更改时,强密码似乎都被标记为弱密码,或者弱密码被标记为强密码。知道我该如何改进吗?

import re 
 
 
pass2Regex = re.compile(r''' 
    \d* 
    [a-zA-Z]* 
    \d* 
    [a-zA-Z]* 
    ''',re.VERBOSE) 
 
 
text = raw_input("enter your password\n") 
if len(text) >= 8: 
    search = pass2Regex.findall(text) 
    if text in search: 
        print "%s is a strong password" % (text) 
    else: 
        print "%s is a weak password" % (text) 
else: 
    print "%s is a weak password" % (text) 

例如,现在如果密码是“231242441”,即使没有字母也会被标记为强密码。此外,当我尝试使用 + 而不是 * 时,它只会接受以数字等开头的密码。

感谢帮助