I am exploring the regular expression library in C++11.I am bit confused about the behavior of 'regex_search()' function which I wanted to clarify. Below is my sample program and it returns the below output
String that matches the pattern:
test 1 2 3 4 5 abc def abc
My query is that, why is it NOT matching the pattern test 1 2 3 4 5 abc
but matching only test 1 2 3 4 5 abc def abc
? Should it not match the first one(ie test 1 2 3 4 5 abc
) also for the given regular expression? Can someone please help me to understand?
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main()
{
std::string inputStr = "test 1 2 3 4 5 abc def abc";
std::string regexPattern = "test 1.*abc";
regex regexp(regexPattern, std::regex::grep);
smatch m;
while(std::regex_search(inputStr, m, regexp, std::regex_constants::match_default))
{
std::cout<<"String that matches the pattern: "<< m.str() << std::endl;
inputStr = m.suffix();
}
return 0;
}