0

Why this is failing ? I am trying to match a substring using regex in C++STL. What I am doing wrong here ?

GCC Version :: (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");
}
Krishna M
  • 1,135
  • 2
  • 16
  • 32

1 Answers1

0

According to the docs std::regex_match only matches the whole string. You probably want 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");
}

Also you should check if your implementation supports std::regex which is a new feature in C++11. For example the GCC compiler only properly implements <regex> in versions 4.9.0 and above.

Galik
  • 47,303
  • 4
  • 80
  • 117