C ++ Qt RegExp与@或|等特殊字符不匹配或^

时间:2013-08-11 10:36:59

标签: c++ regex qt

我目前正在为我的RegExp问题寻找解决方案。我已经完成了文档并浏览了互联网,但似乎没有人使用RegExp - 至少,我找不到我想要的东西。

我目前正在根据SANS机构的密码策略进行密码强度检查。 (http://www.sans.org/security-resources/policies/Password_Policy.pdf

它说强密码包含:

  

“特殊”字符(例如@#$%^& *()_ + |〜 - =`{} []:“;'<> / etc)

所以我想在我的Qt项目中实现这一点。相关代码是:

bool specialChars = contains("[@|#|$|%|\^|&|*|(|)|_|+|\||~|-|=|\|`|{|}|\[|\]|:|\"|;|'|<|>|/]", password);

而包含是:

/**
 * @brief PasswordStrenghChecker::contains
 * @param needle
 * @param hay
 * @return
 */
bool PasswordStrengthChecker::contains(QString needle, QString hay)
{
    QRegExp check(needle);
    for(int i = 0; i < hay.length(); i++)
    {
        if ( check.exactMatch(hay.at(i)) )
        {
            return true ;
        }
    }
    return false;
}

当我检查它显示的结果时,@和任何其他字符不匹配。这里发生了什么事 ?正则表达式适用于任何其他条件,例如:

bool digits = contains("\\d", password);
bool lowerCase = contains("[a-z]", password);
bool upperCase = contains("[A-Z]", password);

我期待着你的帮助。

1 个答案:

答案 0 :(得分:2)

尝试使用反斜杠\转义已发现问题的字符