3

URLからドメインを抽出しようとしています。以下はスクリプトの例です。

#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main () {

  std::string url = "http://mydomain.com/randompage.php";
  boost::regex exp("^https?://([^/]*?)/");
  std::cout << regex_search(url,exp);

}

一致した値を印刷するにはどうすればよいですか?

4

1 に答える 1

8

match_resultsオブジェクトを取得するregex_searchのオーバーロードを使用する必要があります。あなたの場合:

#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main () {    
  std::string url = "http://mydomain.com/randompage.php";
  boost::regex exp("^https?://([^/]*?)/");
  boost::smatch match;
  if (boost::regex_search(url, match, exp))
  {
    std::cout << std::string(match[1].first, match[1].second);
  }    
}

編集:修正された開始、終了==>最初、2番目

于 2010-06-19T04:16:15.410 に答える