C ++ 11 Regex Find Capture Group ID

I looked at several sources for the new C ++ 11 regex library, but most of them are more focused on the syntax or more general use of things like regex_match or regex_search. Although these articles helped me start using the regex library, I find it difficult to find more information about capture groups.

What I'm trying to do is figure out which capture group the match belongs to. So far, I have found the only way to do this.

#include <iostream>
#include <string>
#include <regex>

int main(int argc, char** argv)
{
    std::string input = "+12 -12 -13 90 qwerty";
    std::regex pattern("([+-]?[[:digit:]]+)|([[:alpha:]]+)");

    auto iter_begin = std::sregex_token_iterator(input.begin(), input.end(), pattern, 1);
    auto iter_end = std::sregex_token_iterator();

    for (auto it = iter_begin; it != iter_end; ++it)
    {
        std::ssub_match match = *it;
        std::cout << "Match: " << match.str() << " [" << match.length() << "]" << std::endl;
    }

    std::cout << std::endl << "Done matching..." << std::endl;
    std::string temp;
    std::getline(std::cin, temp);

    return 0;
}

std::sregex_token_iterator, , , , . , , , , , .

, , - , , std::sregex_token_iterator (-, , , , , ).

, ? ?

+5
1

regex_iterator . match_results , sub_match es, , .

+5

All Articles