c++ Url Parser using boost regex match

╄→尐↘猪︶ㄣ 提交于 2019-12-12 13:49:20

问题


how can i parse an url in c++ with boost regex like i have an url

http://www.google.co.in/search?h=test&q=examaple

i need to split the base url www.google.com and then query path search?h=test&q=examaple


回答1:


Are you sure you need regex for that?

#include <iostream>
#include <algorithm>

int main()
{
  using namespace std;
  string x = "http://www.google.co.in/search/search/?h=test&q=examaple";

  size_t sp = x.find_first_of( '/', 7 /* skip http:// part */ );
  if ( sp != string::npos ) {
        string base_url( x.begin()+7, x.begin()+sp );
        cout << base_url << endl;
        sp = x.find_last_of( '/' );
        if ( sp != string::npos ) {
                string query( x.begin()+sp+1, x.end() );
                cout << query << endl;
        }
  }

  return 0;
}

regex version:

string input_string = "http://www.google.co.in/search/search/?h=test&q=examaple";
boost::regex exrp( "^(?:http://)?([^/]+)(?:/?.*/?)/(.*)$" );
boost::match_results<string::const_iterator> what;
if( regex_search( input_string, what, exrp ) ) {
    std::string base_url( what[1].first, what[1].second );
    std::string query( what[2].first, what[2].second );
}


来源:https://stackoverflow.com/questions/3624651/c-url-parser-using-boost-regex-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!