所以我试图在python中包含正则表达式对象的字典上运行for循环,但是我收到以下错误:
============ RESTART: C:/Users/JSurzyn/Scripts/pw_verify_neat.py ============
Enter a password >>>
Jordan93
Traceback (most recent call last):
File "C:/Users/JSurzyn/Scripts/pw_verify_neat.py", line 18, in <module>
for key, value in reg_dict:
TypeError: '_sre.SRE_Pattern' object is not iterable
正则表达式对象是否不可迭代?任何帮助都会很棒。我在Windows 10 64位计算机上运行python 3.6。
上下文。我正在尝试使用以下代码
import re
pw = str(input('Enter a password >>>\n' ))
length_regex = re.compile(r'.{8,}')
upper_regex = re.compile(r'[A-Z]')
lower_regex = re.compile(r'[a-z]')
num_regex = re.compile(r'[0-9]')
def t_pw(pw):
try:
mo = length_regex.search(pw)
fo = mo.group()
try:
mo = upper_regex.search(pw)
fo = mo.group()
try:
mo = lower_regex.search(pw)
fo = mo.group()
try:
mo = num_regex.search(pw)
fo = mo.group()
print('password approved')
except:
print('password must have at least one numerical digit')
except:
print('password must have at least one lower case letter')
except:
print('password must have at least one upper case letter')
except:
print('password must be at least 8 characters.')
t_pw(pw)
并清理它看起来像:
import re
pw = str(input('Enter a password >>>\n' ))
length_regex = re.compile(r'.{8,}')
upper_regex = re.compile(r'[A-Z]')
lower_regex = re.compile(r'[a-z]')
num_regex = re.compile(r'[0-9]')
l_string = 'password must be at least 8 characters.'
u_string = 'password must have at least one upper case letter'
lo_string = 'password must have at least one lower case letter'
n_string = 'password must have at least one numerical digit'
reg_dict = {length_regex : l_string, upper_regex : u_string,
lower_regex : lo_string, num_regex : n_string}
for key, value in reg_dict:
try:
mo = key.search(pw)
fo = mo.group()
except:
print(value)
exit()
print('Password approved')