Reputation: 138
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();
}
Upvotes: 1
Views: 1752
Reputation: 138
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);
}
Upvotes: 3