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);
  }    
}

编辑:更正开始,结束==>第一,第二

于 2010-06-19T04:16:15.410 回答