【发布时间】:2019-11-08 16:09:02
【问题描述】:
我正在尝试编写一个检查强密码的函数。密码必须包含一个大写,一个小写,一个数字,并且必须是8个字符。
import re
def checker():
while True:
userInput = input(' Please input a password ')
passwordRegex = re.compile(r'[a-zA-Z0-9]+ {,8}')
match = passwordRegex.search(userInput)
if match:
print('Good!')
else:
print('Bad!')
checker()
即使密码满足所有要求,此函数也始终输出Bad。我感觉这个错误与我使用正则表达式和变量的方式有关。我正在使用python 3.6。
【问题讨论】:
-
这与变量无关。只是 1)您的正则表达式错误,2)您使用的正则表达式错误(
re.search)。 -
请注意,您的正则表达式尝试按字面意思匹配`{,8}`
-
感谢您的反馈@Aran-Fey,我已根据您的建议更新了我的代码。