The problem is actually that you're mixing iterator types (std::string::iterator
and std::string::const_iterator
, and since regex_search
is a template function, the implicit conversion from iterator
to const_iterator
is disallowed.
You are correct that declaring test
as a const std::string
would fix it, as test.end()
would now return a const_iterator
, instead of iterator
.
Alternately, you can do do:
std::string test = "1234567890";
boost::regex testPattern( "\\d" );
boost::match_results<std::string::const_iterator> testMatches;
std::string::const_iterator startPos = test.begin();
std::string::const_iterator endPos = test.end();
while( regex_search( startPos, endPos, testMatches, testPattern ) ) {
// Do stuff: record match value, increment start position
}
If you've C++11 available, you could also use new std::string::cend
member:
std::string test = "1234567890";
boost::regex testPattern( "\\d" );
boost::match_results<std::string::const_iterator> testMatches;
std::string::const_iterator startPos = test.begin();
while( regex_search( startPos, test.cend(), testMatches, testPattern ) ) {
// Do stuff: record match value, increment start position
}