题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
正则表达式验证密码是否合格
import re def validate(key: str): # 匹配任何非空白字符,不包括换行 pattern = re.compile(r'(\S){8,}') # 匹配小写字母 lower = re.compile(r'[a-z]') # 匹配大写字母 upper = re.compile(r'[A-Z]') # 匹配数字 digit = re.compile(r'\d') # 匹配其他字符 other = re.compile(r'[^A-Za-z0-9]') a, b, c, d = 0, 0, 0, 0 if re.match(pattern, key): if re.search(lower, key): a = 1 if re.search(upper, key): b = 1 if re.search(digit, key): c = 1 if re.search(other, key): d = 1 if a + b + c + d < 3: return False for i in range(len(key) - 3): if len(key.split(key[i:i+3])) >= 3: return False return True while 1: try: print('OK' if validate(input()) else 'NG') except: break