为什么这是失败的?我正在尝试在C++STL中使用正则表达式匹配子字符串。我在这里做错了什么?
GCC版本::(Linaro GCC 4.8-2014.04) 4.8.3
#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");
}发布于 2015-05-08 03:07:29
根据文档,std::regex_match只匹配整个字符串。您可能需要std::regex_search
#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>。
https://stackoverflow.com/questions/30109616
复制相似问题