使用正则表达式进行输入验证

时间:2014-11-17 04:43:55

标签: c++ regex c++11

我知道以下不是最有效的实现(毕竟,我可以在main()中运行regex_match())但它仍然可以工作,至少从我有限的理解。

我试图在C ++ 11中使用来验证dicerolls的一些输入。我用expresso来构建正则表达式,并在expresso中验证它就好了。为什么它会在这段代码中失败?

#include <iostream>
#include <string>
#include <regex>

bool regexValidate (string expr, string teststring) 
{
    regex ex(expr);
    if (regex_match(teststring,ex)) {
        return true;
        cout << "true";
    } else if (regex_match (teststring,regex("^\s*\d*d\d*\s*$"))) {
        return true;
        cout << "true";
    }
    return false;
}

int main() 
{
    char    choice;             // menu option selected by user
    bool    isExit = false;     // Is the user ready to exit?         

    // test code
    string  diceexpr = "^\s*\d{1,}d\d{1,}\s*$";
    string  teststr = "10d10";
    string  teststr1 = "1d10";
    string  teststr2 = " 10d10 ";

    cout << teststr << " is ";
    if (regexValidate(diceexpr,teststr)) {
        cout << " valid!" << endl;
    } else {
        cout << " invalid!" << endl;
    }
    cout << teststr1 << " is ";
    if (regexValidate(diceexpr,teststr1)) {
        cout << " valid!" << endl;
    } else {
        cout << " invalid!" << endl;
    }
    cout << teststr2 << " is ";
    if (regexValidate(diceexpr,teststr2)) {
        cout << " valid!" << endl;
    } else {
        cout << " invalid!" << endl;
    }
    return 0;
}

示例输出:

10d10 is  invalid!
1d10 is  invalid!
 10d10  is  invalid!

RUN SUCCESSFUL (total time: 291ms)

更新

更正了我的代码并将其更改为使用boost库进行正则表达式。

#include <iostream>
#include <string>
#include <boost/regex.hpp>
using namespace std;

bool regexValidate (string expr, string teststring) 
{
    boost::regex ex(expr);
    if ( boost::regex_match (teststring,ex) ) {
        cout << "true";
        return true;
    //} else if (regex_match (teststring,regex("^\s*\d*d\d*\s*$"))) {
    //    cout << "true";
    //    return true;
    }
    return false;
}

int main()
{
    string  diceexpr = "^\\s*\\d{1,}d\\d{1,}\\s*$";
    string  teststr = "10d10";
    string  teststr1 = "1d10";
    string  teststr2 = " 10d10 ";

    cout << teststr << " is ";
    if (regexValidate(diceexpr,teststr)) {
        cout << " valid!" << endl;
    } else {
        cout << " invalid!" << endl;
    }
    cout << teststr1 << " is ";
    if (regexValidate(diceexpr,teststr1)) {
        cout << " valid!" << endl;
    } else {
        cout << " invalid!" << endl;
    }
    cout << teststr2 << " is ";
    if (regexValidate(diceexpr,teststr2)) {
        cout << " valid!" << endl;
    } else {
        cout << " invalid!" << endl;
    }
    return 0;
}

给出以下输出:

terminate called after throwing an instance of 'std::regex_error'
  what():  regex_error

RUN FAILED (exit value 1, total time: 1s)

1 个答案:

答案 0 :(得分:2)

  

string diceexpr =&#34; ^ \ s * \ d {1,} d \ d {1,} \ s * $&#34 ;;

这并不是你认为它做的。在字符串文字中,"\s""s"相同(您应该看到有关无法识别的转义序列的编译器警告),而"\\s"代表两个字符,反斜杠和's'

将所有反斜杠加倍,或者使用正则表达式的原始字符串文字,如

string  diceexpr = R"(^\s*\d{1,}d\d{1,}\s*$)";