I have an issue accessing sub-matches from boost::match_results class. When I examine the program in debugger, match_results::m_subs array contains exactly what I expect:
However, when I try to acces the submatches using operator[] and index of the submatch starting with 1, I don't get what I want. The reason is hidden in boost source:
const_reference operator[](int sub) const
{
if(m_is_singular && m_subs.empty())
raise_logic_error();
sub += 2; //< WTF?
if(sub < (int)m_subs.size() && (sub >= 0))
{
return m_subs[sub];
}
return m_null;
}
I'm totally confused about this. The documentation says I just access n-th submatch using [n], but in code, there's this weird offset everywhere.
Please tell me I'm not insane :)
Checked boost versions: 1.54 and 1.53
The first two elements in the m_subs
vector property of the boost::match_results
class defined in match_results.hpp
are reserved for storing the prefix and suffix. Their exact meanings are:
m_subs[0] - suffix
m_subs[0].first - the end position of the match
m_subs[0].second - the end position of the input text
m_subs[0].matched - m_subs[0].first != m_subs[0].second
m_subs[1] - prefix
m_subs[1].first - the start position of the input text
m_subs[1].second - the start position of the match
m_subs[1].matched - m_subs[1].first != m_subs[1].second
The match positions of capture group $0 are stored in m_subs[2], $1 in m_subs[3], etc, which can be referenced through the match_results class via [0], [1], etc. That is why you can see the magic number 2 added in several places.
Have a look at how match_results' suffix
and prefix
methods are implemented:
const_reference prefix() const
{
if(m_is_singular)
raise_logic_error();
return (*this)[-1];
}
const_reference suffix() const
{
if(m_is_singular)
raise_logic_error();
return (*this)[-2];
}
Since this has been so for quite a while I would not be quick to assume this is causing your particular problem. If you need more help please ask another question containing a SSCCE outlining your problem.
P.S. you're not insane (the above is just really terrible code)