ARTICLE AD BOX
According to regex standards (all implementations), the already-consumed input cannot match subsequent atoms.
I.e. at this point
twonetwo ^the o was already matched and consumed, meaning it cannot match one anymore.
I think you need a better name for your function, and perhaps also express it a bit more efficiently:
std::vector<std::string_view> find_all_regex_with_overlap(std::string_view line, std::regex const& r) { std::vector<std::string_view> res; for (std::cmatch match; // std::regex_search(line.begin(), line.end(), match, r); // line = {match[0].first + 1, line.end()}) // { res.emplace_back(match[0].first, match[0].second); } return res; }See it Live On Compiler Explorer
#include <fmt/ranges.h> #include <regex> namespace my { std::vector<std::string_view> find_all_regex_with_overlap(std::string_view line, std::regex const& r) { std::vector<std::string_view> res; for (std::cmatch match; // std::regex_search(line.begin(), line.end(), match, r); // line = {match[0].first + 1, line.end()}) // { res.emplace_back(match[0].first, match[0].second); } return res; } } // namespace my int main() { static std::vector<std::string> const expected = {"two", "one", "two"}; static std::string const input = "twonetwo"; auto actual = my::find_all_regex_with_overlap(input, std::regex{R"(one|two)"}); fmt::print("expected: {} actual: {}\n", expected, actual); //assert(actual == expected); }Prints
expected: ["two", "one", "two"] actual: ["two", "one", "two"]