Search code examples
c++stringboosttcp

C++, boost: which is fastest way to parse string like tcp://adr:port/ into address string and one int for port?


we have std::string A with tcp://adr:port/ How to parse it into address std::string and one int for port?


Solution

  • void extract(std::string const& ip, std::string& address, std::string& service)
    {
       boost::regex e("tcp://(.+):(\\d+)/");
       boost::smatch what;
       if(boost::regex_match(ip, what, e, boost::match_extra))
       {
         boost::smatch::iterator it = what.begin();
         ++it; // skip the first entry..
         address = *it;
         ++it;
         service = *it;
       }
    }
    

    EDIT: reason service is a string here is that you'll need it as a string for resolver! ;)