fork download
  1. #include <iostream>
  2. #include <regex>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. const std::regex time_regex("(\\d|[0,1]\\d|2[0-3]):([0-5]\\d):([0-5]\\d)");
  8. std::smatch time_match;
  9. std::string line;
  10.  
  11. while (std::getline(std::cin, line))
  12. {
  13. if (std::regex_match(line, time_match, time_regex))
  14. {
  15. int hours = std::stoi(time_match[1]);
  16. int minutes = std::stoi(time_match[2]);
  17. int seconds = std::stoi(time_match[3]);
  18. std::cout << "h=" << hours << " m=" << minutes << " s=" << seconds << std::endl;
  19. }
  20. else
  21. {
  22. std::cout << "Invalid time: " << line << std::endl;
  23. }
  24. }
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 15336KB
stdin
12:49:00
1:45:00
23:59:59
12-49-00
123:45:08
1:2:3
24:00:00
23:60:00
23:59:60
stdout
h=12 m=49 s=0
h=1 m=45 s=0
h=23 m=59 s=59
Invalid time: 12-49-00
Invalid time: 123:45:08
Invalid time: 1:2:3
Invalid time: 24:00:00
Invalid time: 23:60:00
Invalid time: 23:59:60