fork(1) download
  1. #include <iostream>
  2. #include <regex>
  3.  
  4. std::string exptract_phone_number(const std::string& input)
  5. {
  6. const std::regex pattern(R"(^(8|\+?420)\s*\-?\s*(?:(\d{3})|\((\d{3})\))\s*\-?\s*(\d{7})$)");
  7. std::smatch match;
  8. return std::regex_match(input, match, pattern) ?
  9. match[2].str() + match[3].str() + match[4].str() : "";
  10. }
  11.  
  12. int main()
  13. {
  14. std::cout << exptract_phone_number("4207743822140") << std::endl;
  15. std::cout << exptract_phone_number("+420-7743822140") << std::endl;
  16. std::cout << exptract_phone_number("+420-(774)3822140") << std::endl;
  17. std::cout << exptract_phone_number("+420774-3822140") << std::endl;
  18. std::cout << exptract_phone_number("+420(774)-3822140") << std::endl;
  19. std::cout << exptract_phone_number("+420-774-3822140") << std::endl;
  20. std::cout << exptract_phone_number("+420-(774)-3822140") << std::endl;
  21.  
  22. std::cout << exptract_phone_number("87743822140") << std::endl;
  23. std::cout << exptract_phone_number("8-7743822140") << std::endl;
  24. std::cout << exptract_phone_number("8-(774)3822140") << std::endl;
  25. std::cout << exptract_phone_number("8774-3822140") << std::endl;
  26. std::cout << exptract_phone_number("8(774)-3822140") << std::endl;
  27. std::cout << exptract_phone_number("8-774-3822140") << std::endl;
  28. std::cout << exptract_phone_number("8-(774)-3822140") << std::endl;
  29. }
Success #stdin #stdout 0.01s 4536KB
stdin
Standard input is empty
stdout
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140
7743822140