fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4.  
  5. int find_year(std::string line, unsigned index = 0)
  6. {
  7. std::smatch match;
  8. std::regex expr("\\b([0-9]{4})\\b");
  9.  
  10. if ( std::regex_search(line,match,expr) )
  11. return std::stoi(match[index]);
  12. else
  13. throw std::invalid_argument("No number matching a year found!");
  14. }
  15.  
  16. int main()
  17. {
  18. int year = find_year("Matthew Alan Aberegg 1963 452,627");
  19. std::cout << year << "\n";
  20. }
  21.  
Success #stdin #stdout 0s 16168KB
stdin
Standard input is empty
stdout
1963