fork(4) download
  1. #include <string>
  2. #include <iostream>
  3. #include <regex>
  4. using namespace std;
  5.  
  6. int main() {
  7. std::regex r("(a)|(b)|(c)");
  8. std::string s = "abcab";
  9. std::vector<std::string> astrings;
  10. std::vector<std::string> bstrings;
  11. std::vector<std::string> cstrings;
  12.  
  13. for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
  14. i != std::sregex_iterator();
  15. ++i)
  16. {
  17. std::smatch m = *i;
  18. if (m[1].matched) {
  19. astrings.push_back(m[1].str());
  20. std::cout << "1:" << " Added m[1]: " << m[1].str() << " at Position " << m.position(1) << '\n';
  21. }
  22. else if (m[2].matched) {
  23. bstrings.push_back(m[2].str());
  24. std::cout << "2:" << " Added m[2]: " << m[2].str() << " at Position " << m.position(2) << '\n';
  25. }
  26. else if (m[3].matched) {
  27. cstrings.push_back(m[3].str());
  28. std::cout << "3:" << " Added m[3]: " << m[3].str() << " at Position " << m.position(3) << '\n';
  29. }
  30. }
  31. // Printing vectors
  32. for (auto i: astrings)
  33. std::cout << i << ' ';
  34. std::cout << "\n";
  35. for (auto i: bstrings)
  36. std::cout << i << ' ';
  37. std::cout << "\n";
  38. for (auto i: cstrings)
  39. std::cout << i << ' ';
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 3544KB
stdin
Standard input is empty
stdout
1:    Added m[1]: a at Position 0
2:    Added m[2]: b at Position 1
3:    Added m[3]: c at Position 2
1:    Added m[1]: a at Position 3
2:    Added m[2]: b at Position 4
a a 
b b 
c