首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >此regex_match函数不能正确工作,错误代码7。

此regex_match函数不能正确工作,错误代码7。
EN

Stack Overflow用户
提问于 2015-10-16 19:23:18
回答 1查看 33关注 0票数 0

这是我正在使用的代码

代码语言:javascript
复制
string exptype(string s)
{
string type = "";
try
{
    if (regex_match(s.c_str(), regex("101(0|1)*111")))
        type = "a valid binary";
    else if (regex_match(s.c_str(), regex("[a-zA-Z]")))
        type = "a valid combination of alphanum letters";
    else if (regex_match(s.c_str(), regex("\([0-9]{3}\)[0-9]{3}-{0-9}{4}")))
        type = "a valid phone number";
    else if (regex_match(s.c_str(), regex("(19|20)[0-9][0-9]-(0[1-9]|1[0-2])-   ((0[1-9])|([1-2][0-9])|(3[0-1]))")))
        type = "a valid date";
    else
        type = "an invalid string";
}
catch (std::regex_error& e) 
{
    cout << e.code() << endl;
}


    return type;
}

然后我的主要内容如下:

代码语言:javascript
复制
int main()
{
string input;
do 
{
    cout << "Enter the string that will be validated.." << endl;
    getline(cin, input);
    if (input != "q")
    {
        cout << "This is " << exptype(input) << endl;
    }
    else
        break;

} while (true);

return 0;
}

上述代码有时会工作,有时它会抛出错误代码的异常:7我搜索了它,发现错误是error_brace:包含不匹配大括号({和})的表达式。我看不出这里有什么问题,任何帮助都是值得感激的。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-16 19:45:10

正如马里亚诺所指出的,您应该用{0-9}替换[0-9]。在处理正则表达式或其他带有转义\的字符串时,最好使用原始字符串。例如:

代码语言:javascript
复制
regex_match(s.c_str(), regex(R"reg(\([0-9]{3}\)[0-9]{3}-[0-9]{4})reg"))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33178152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档