【发布时间】:2021-07-26 19:05:01
【问题描述】:
我想测试输入值是否符合条件:
- 至少一个小写字母
- 至少一个大写字母
- 至少一位数
- 至少有一个字符不是 \w
我编写的正则表达式似乎只遵循这个特定的顺序,例如: abCD99$%
但如果我打乱了序列,正则表达式就不再起作用了: CD99ab$%
请问有大神知道是什么问题吗?提前干杯。
import re
# Asks user for an input
print('Please enter a password for checking its strength:')
pwd = input('> ')
#Test the input to see if it is more than 8 characters
if not len(pwd) < 8:
pwdRegex = re.compile(r'([a-z]+)([A-Z]+)([0-9]+)(\W+)') #order problem
if not pwdRegex.search(pwd) == None:
print('Password OK.')
else:
print('Please make sure password fulfills requirements!')
else:
print('Characters must not be less than 8 characters!')
【问题讨论】:
-
正确使用正向前瞻可以解决该问题。我认为在 SO 上有很多例子。
-
为每个标准使用单独的正则表达式要容易得多。
-
@Thomas 仅在您不熟悉正则表达式时使用 :-)