【Python】python re模块判断密码的条件

发布时间 2023-12-19 16:23:46作者: 是阿杰呀

判断密码长度最少8位, 且应为数字、字母和特殊符号中至少2类的组合

import re


def check_password_func(password):
    # 长度至少为8位,且应为数字、字母和特殊符号中至少2类的组合
    match1 = bool(re.search(r'\d+', password))
    match2 = bool(re.search(r'[a-zA-Z]+', password))
    match3 = bool(re.search(r'[!@#$%^&*(),.?":{}|<>]+', password))
    match4 = bool(re.match(r'^.{8,}$', password))
    bool_1 = (match1 and match2) or (match1 and match3) or (match2 and match3)
    return bool_1 and match4