fork(1) download
  1. #include <iostream>
  2. #include <regex>
  3. #include <string>
  4.  
  5. std::string extract_authority(std::string uri)
  6. {
  7. // See: Apendix B from http://w...content-available-to-author-only...f.org/rfc/rfc2396.txt for regular expression
  8. std::regex uri_exp(R"xxx(^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)xxx");
  9. std::smatch matches;
  10.  
  11. if (std::regex_match(uri, matches, uri_exp) && matches.size() >= 5)
  12. return matches[4].str();
  13.  
  14. return std::string();
  15. }
  16.  
  17. std::string trim_prefix(std::string s, std::string prefix)
  18. {
  19. return s.find(prefix) == 0 ? s.substr(prefix.length()) : s;
  20. }
  21.  
  22. int main()
  23. {
  24. std::vector<std::string> examples =
  25. {
  26. R"(http://w...content-available-to-author-only...i.edu/pub/ietf/uri/#Related)",
  27. R"(http://w...content-available-to-author-only...s.com/user/)",
  28. R"(ftp://f...content-available-to-author-only...o.za/rfc/rfc1808.txt)",
  29. R"(gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles")",
  30. R"(http://w...content-available-to-author-only...o.no/faq/compression-faq/part1.html)",
  31. R"(mailto:mduerst@ifi.unizh.ch)",
  32. R"(news:comp.infosystems.www.servers.unix")",
  33. R"(telnet://melvyl.ucop.edu/)",
  34. };
  35.  
  36. for (auto example : examples)
  37. {
  38. auto auth = extract_authority(example);
  39.  
  40. std::cout << '"' << trim_prefix(auth, "www.") << "\" (" << example << ")\n";
  41. }
  42. }
Success #stdin #stdout 0s 3544KB
stdin
Standard input is empty
stdout
"ics.uci.edu" (http://w...content-available-to-author-only...i.edu/pub/ietf/uri/#Related)
"cplusplus.com" (http://w...content-available-to-author-only...s.com/user/)
"ftp.is.co.za" (ftp://f...content-available-to-author-only...o.za/rfc/rfc1808.txt)
"spinaltap.micro.umn.edu" (gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles")
"math.uio.no" (http://w...content-available-to-author-only...o.no/faq/compression-faq/part1.html)
"" (mailto:mduerst@ifi.unizh.ch)
"" (news:comp.infosystems.www.servers.unix")
"melvyl.ucop.edu" (telnet://melvyl.ucop.edu/)