You should do this to get all substring:
while (std::regex_search (str,res,rx)) {
std::cout <<res[0] << std::endl;
str = res.suffix().str();
}
Or you can use std::regex_iterator to get all substrings as below:
std::regex_iterator<std::string::iterator> rit ( str.begin(), str.end(), rx);
std::regex_iterator<std::string::iterator> rend;
while (rit != rend) {
std::cout << rit->str() << std::endl;
++rit;
}
But it will still output '101' and '1000001' when the string is "00110101000001" because the first match consumes part of the string. If you want to find all overlapping matches, then you need a regex implementation which supports Lookaround Assertion. Python does:
>>> re.findall(r'(?=(1[0]+1))', '00110101000001')
['101', '101', '1000001']
(?=...)
Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.