I've found an example at cpluplus.com. Here it is:
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("This subject has a submarine as a subsequence");
std::smatch m;
std::regex e ("\\b(sub)([^ ]*)"); // Matches words beginning by "sub"
std::cout << "Target sequence: " << s << std::endl;
std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
std::cout << "The following matches and submatches were found:" << std::endl;
while (std::regex_search (s,m,e)) {
for (auto x:m)
std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
return 0;
}
Why can we use a for-each
loop on the object of the type smatch
?
I'm used to using the foreach loop only with STL-containers...