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

regex_token_iterator takes an optional fourth argument specifying which submatch is returned for each iteration. The default value of this argument is 0, which in case of the C++ (and many other) regexes means "the whole match". If you want to get the first captured submatch, simply pass 1 to the constructor:

const std::sregex_token_iterator End;
Expression.assign("rel=\"nofollow\">(.*?)</a>");
for (std::sregex_token_iterator i(Tables.begin(), Tables.end(), Expression, 1); i != End; ++i)
{
    std::cout << *i << std::endl; // *i only yields the captured part
}

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\">(.*?)</a>");
for (std::sregex_iterator i(Tables.begin(), Tables.end(), Expression); i != End; ++i)
{
    std::cout << (*i)[1] << std::endl; // first submatch, same as above.
}

Tags:

C++

Regex

Stl

C++11