There is a line:

string s1 = "20.02.1912 and 02.08.1756." 

And there is a pattern:

 regex date_pat1{ R"((\d{2})\.(\d{2})\.(\d{4}))" }; 

It is necessary to have access to the first date (substring), and the second date, and the n-th date, if there is one in the line. How can this be done if regex_search () finds only the first substring matching this pattern?

    1 answer 1

    Use std::sregex_iterator , and place all found dates in std::vector<std::string> :

     #include <string> #include <iostream> #include <regex> int main() { std::regex date_pat1{ R"(\d{2}\.\d{2}\.\d{4})" }; std::string s = "20.02.1912 and 02.08.1756."; std::vector<std::string> res; for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), date_pat1); i != std::sregex_iterator(); ++i) { std::smatch m = *i; res.push_back(m.str()); } // Демо for (auto i: res) { std::cout << i << std::endl; } return 0; } 

    See the online demo .

    Result:

     20.02.1912 02.08.1756