密码验证程序:密码是否必须包含一个空格字符?

时间:2018-10-04 21:24:59

标签: python python-3.x

public interface DAO {

    @Query("SELECT * from " + 
        "(LEFT JOIN item ON task.id = item.taskId) " +
        "LEFT JOIN sub_item ON item.id = sub_item.itemId " + 
        "WHERE task.id=:id")
    List getTask(int id);

}

这是我的代码:

Task: Password validator
a. The length of the password needs to be from 8 to 30 characters.
b. The password needs to contain at least 2 uppercase characters.
c. The password needs to contain at least 2 lowercase characters.
d. The password needs to contain at least 1 digit.
e. The password needs to contain exactly 1, but not more of the following special characters:
@/%&*_-

有效。但是,条件e)尚未完全满足。如何使密码仅包含一个特殊字符?

2 个答案:

答案 0 :(得分:1)

len + re.findall()

...
elif len(re.findall('[@/%&*_-]', password)) != 1:
    # Some number of special characters other than 1 was found
    ...

...
elif len(re.findall('[@/%&*_-]', password)) == 0:
    # No special characters were found
    ...
elif len(re.findall('[@/%&*_-]', password)) > 1:
    # Too many special characters were found

(如果您选择后者,请尽量不要运行两次正则表达式)

答案 1 :(得分:1)

请勿为此使用正则表达式;在建议的密码中添加特殊字符的数量:

>>> passwd = "My_Pass@"
>>> special = '[@/%&*_-]'
>>> special_count = sum(passwd.count(c) for c in special)
>>> special_count
2

您现在可以根据需要使用special_count

相关问题