虽然您已经有了答案,但我什至会尝试优化该模式。代替.*
然后回溯,我会应用对比原理直接地:
(?=\D*\d) # NOT a number, 0+ times, then one number
(?=[^A-Z]*[A-Z]) # NOT an UPPERCASE, 0+times, then an UPPERCASE
(?=[^a-z]*[a-z]) # same with lowercase
^[A-Za-z0-9]{10,}$ # allowed characters, 10+, with anchors on both sides
浓缩和demo:
(?=\D*\d)(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])[A-Za-z0-9]{10,}$
这里的想法是,虽然.*
让你顺势而下,然后又原路返回,上面的模式可能会更快结束。
At last the
Python
snippet:
import re
def golf(password=None):
rx = re.compile(r'(?=\D*\d)(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])[A-Za-z0-9]{10,}$')
return True if rx.match(password) else False
passwords = ['A1213pokl', 'bAse730onE', 'asasasasasasasaas', 'QWERTYqwerty', '123456123456', 'QwErTy911poqqqq', '..........']
vectors = [golf(password) for password in passwords]
print(vectors)
# [False, True, False, False, False, True, False]