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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How do I loop through results from std::regex_search? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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

Expression.assign("rel="nofollow">(.*?)</a>");
if (std::regex_search(Tables, Match, Expression))
{
    for (std::size_t i = 1; i < Match.size(); ++i)
        std::cout << Match[i].str() << std::endl;
}

So I tried to do it another way – with an iterator:

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

This does go through every match, but it also gives me the whole matching string instead of just the capture that I was after. Surely must be another way than having to do another std::regex_search on the iterator element in the loop?

Thanks in advance.

Answer

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
}
We are here to answer your question about How do I loop through results from std::regex_search? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji