How do I loop through results from std::regex_search?

后端 未结 2 698
孤街浪徒
孤街浪徒 2021-01-01 22:45

After calling std::regex_search, I\'m only able to get the first string result from the std::smatch for some reason:

Expression.ass         


        
2条回答
  •  醉话见心
    2021-01-01 23:34

    std::regex_search searches for the regex just once. It does not return a list of matches, but a list of submatched expressions (those within parentheses). This is why you only get one Match[1], the text inside the link tag.

    As for the second code, it actually returns you all the matches, but it returns you again match_results object, so you have to use the [] operator:

    const std::sregex_iterator End;
    Expression.assign("rel=\"nofollow\">(.*?)");
    for (std::sregex_iterator i(Tables.begin(), Tables.end(), Expression); i != End; ++i)
    {
        std::cout << (*i)[1] << std::endl; // first submatch, same as above.
    }
    

提交回复
热议问题