C++ 无法使用正则表达式搜索匹配子字符串

C++ 无法使用正则表达式搜索匹配子字符串,c++,regex,stl,C++,Regex,Stl,为什么会失败?我试图在C++STL中使用正则表达式匹配子字符串。 我做错了什么 GCC版本:(Linaro GCC 4.8-2014.04)4.8.3 #包括 使用名称空间std; int main() { regex e(“auth”); smatch m; string s=“连接到a:b:c:d完成的身份验证id=3,str=3”; //字符串s=“auth”; 布尔匹配=正则表达式搜索(s,e); 如果(匹配==真) printf(“匹配”); 其他的 printf(“不匹配”); }

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

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

#包括
使用名称空间std;
int main()
{
regex e(“auth”);
smatch m;
string s=“连接到a:b:c:d完成的身份验证id=3,str=3”;
//字符串s=“auth”;
布尔匹配=正则表达式搜索(s,e);
如果(匹配==真)
printf(“匹配”);
其他的
printf(“不匹配”);
}
根据文档,仅匹配整个字符串。你可能想要

#包括
使用名称空间std;
int main()
{
regex e(“auth”);
smatch m;
string s=“连接到a:b:c:d完成的身份验证id=3,str=3”;
//字符串s=“auth”;
布尔匹配=正则表达式搜索(s,e);
如果(匹配==真)
printf(“匹配”);
其他的
printf(“不匹配”);
}

您还应该检查您的实现是否支持
std::regex
,这是
C++11
中的一个新特性。例如,
GCC
编译器仅在
4.9.0
及以上版本中正确实现

我也使用了regex\u搜索。它给了我同样的感觉result@KrishnaM这段代码适合我,也许你的编译器不完全支持正则表达式?如果使用GCC需要v4.9.0或更高版本,则不确定是否有其他编译器。3@KrishnaM这就是问题所在:)
#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");
}
#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");
}