fork download
  1. /*
  2.   Regex para validar determinado formato de data
  3.   https://pt.stackoverflow.com/q/272539/53463
  4. */
  5.  
  6. #include <iostream>
  7. #include <regex>
  8.  
  9. int main() {
  10. constexpr char text[]{"29/feb/2020"};
  11. std::regex re(R"((?:(?:(0?[1-9]|1\d|2[0-8])([-/.])(0?[1-9]|1[0-2]|j(?:an|u[nl])|ma[ry]|a(?:pr|ug)|sep|oct|nov|dec|feb)|(29|30)([-/.])(0?[13-9]|1[0-2]|j(?:an|u[nl])|ma[ry]|a(?:pr|ug)|sep|oct|nov|dec)|(31)([-/.])(0?[13578]|1[02]|jan|ma[ry]|jul|aug|oct|dec))(?:\2|\5|\8)(0{2,3}[1-9]|0{1,2}[1-9]\d|0?[1-9]\d{2}|[1-9]\d{3})|(29)([-/.])(0?2|feb)\12(\d{1,2}(?:0[48]|[2468][048]|[13579][26])|(?:0?[48]|[13579][26]|[2468][048])00)))");
  12. std::cmatch match;
  13. bool valid = std::regex_match(text, match, re);
  14.  
  15. if (valid) {
  16. std::cout << "Data válida: " << match[0] << std::endl
  17. << "Dia: " << match[1] << match[4] << match[7] << match[11] << std::endl
  18. << "Mês: " << match[3] << match[6] << match[9] << match[13] << std::endl
  19. << "Ano: " << match[10] << match[14] << std::endl;
  20. } else {
  21. std::cout << "Data inválida!!";
  22. }
  23. return 0;
  24. }
Success #stdin #stdout 0s 4436KB
stdin
Standard input is empty
stdout
Data válida: 29/feb/2020
Dia: 29
Mês: feb
Ano: 2020