fork(3) download
  1. #include <string>
  2. #include <iostream>
  3. #include <regex>
  4. using namespace std;
  5.  
  6. int main() {
  7. std::regex r(R"((\b[[:alpha:]]+\b)|(\b\d+\b))");
  8. std::string s = "foo bar 123";
  9. for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
  10. i != std::sregex_iterator();
  11. ++i)
  12. {
  13. std::smatch m = *i;
  14. std::cout << "Match value: " << m.str() << " at Position " << m.position() << '\n';
  15.  
  16. for(auto index = 1; index < m.size(); ++index ){
  17. if (m[index].matched) {
  18. std::cout << "Capture group ID: " << index-1 << std::endl;
  19. break;
  20. }
  21. }
  22. }
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5460KB
stdin
Standard input is empty
stdout
Match value: foo at Position 0
Capture group ID: 0
Match value: bar at Position 4
Capture group ID: 0
Match value: 123 at Position 8
Capture group ID: 1