首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用regex_search匹配子字符串

无法使用regex_search匹配子字符串
EN

Stack Overflow用户
提问于 2015-05-08 03:04:43
回答 1查看 41关注 0票数 0

为什么这是失败的?我正在尝试在C++STL中使用正则表达式匹配子字符串。我在这里做错了什么?

GCC版本::(Linaro GCC 4.8-2014.04) 4.8.3

代码语言:javascript
复制
#include<regex>
using namespace std;
int main()
{
        regex e("auth");
        smatch m;
        string s="Connected to a:b:c:d completed auth id=3, str=3";
        //string s="auth";

        bool match = regex_search(s,e);
        if( match == true )
                printf("matched");
        else
                printf("no match");
}
EN

回答 1

Stack Overflow用户

发布于 2015-05-08 03:07:29

根据文档,std::regex_match只匹配整个字符串。您可能需要std::regex_search

代码语言:javascript
复制
#include<regex>
using namespace std;
int main()
{
        regex e("auth");
        smatch m;
        string s="Connected to a:b:c:d completed auth id=3, str=3";
        //string s="auth";

        bool match = regex_search(s,e);
        if( match == true )
                printf("matched");
        else
                printf("no match");
}

此外,您应该检查您的实现是否支持std::regex,这是C++11中的一个新特性。例如,在4.9.0和更高版本中,GCC编译器只能正确地实现<regex>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30109616

复制
相关文章

相似问题

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