#include <iostream>
#include <regex>
#include <string>

std::string extract_authority(std::string uri)
{
    // See: Apendix B from http://w...content-available-to-author-only...f.org/rfc/rfc2396.txt for regular expression
    std::regex uri_exp(R"xxx(^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)xxx");
    std::smatch matches;

    if (std::regex_match(uri, matches, uri_exp) && matches.size() >= 5)
        return matches[4].str();

    return std::string();
}

std::string trim_prefix(std::string s, std::string prefix)
{
    return s.find(prefix) == 0 ? s.substr(prefix.length()) : s;
}

int main()
{
    std::vector<std::string> examples =
    {
        R"(http://w...content-available-to-author-only...i.edu/pub/ietf/uri/#Related)",
        R"(http://w...content-available-to-author-only...s.com/user/)",
        R"(ftp://f...content-available-to-author-only...o.za/rfc/rfc1808.txt)",
        R"(gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles")",
        R"(http://w...content-available-to-author-only...o.no/faq/compression-faq/part1.html)",
        R"(mailto:mduerst@ifi.unizh.ch)",
        R"(news:comp.infosystems.www.servers.unix")",
        R"(telnet://melvyl.ucop.edu/)",
    };

    for (auto example : examples)
    {
        auto auth = extract_authority(example);

        std::cout << '"' << trim_prefix(auth, "www.") << "\" (" << example << ")\n";
    }
}