How can I find position in string for submatch in boost::regex

1.7k Views Asked by At

after boost::regex_search(start, end, what, pattern) I can find start position of full match in search string by calling what.position().

How can I find those positions for submatches?

I need to have code like this:

if(boost::regex_search(start, end, what, pat))
{
    int len = what["namedGroup"].length();
    int pos = what["namedGroup"].position();
}
1

There are 1 best solutions below

0
On BEST ANSWER

I found the solution.

Each element of boost::match_results, in my case boost::match_results of std::string::const_iterator, has property first and second, which points correspondingly to begin and end iterators for this submatch in search string. So you can use iterators or convert them to indices via std::distance()

std::string::const_iterator start, end;
boost::match_results<std::string::const_iterator> what;
start = searchString.begin();
end  = searchString.end ();

if(boost::regex_search(start, end, what, pattern))
    {
        std::string::const_iterator beg = what["namedGroup"].first;
        std::string::const_iterator end = what["namedGroup"].second;
        int beginIndex = std::distance(start, beg);
    }