C++ 如何使用Boost.regex获得正则表达式匹配的值?
我正在尝试从URL提取域。下面是一个示例脚本C++ 如何使用Boost.regex获得正则表达式匹配的值?,c++,regex,boost-regex,C++,Regex,Boost Regex,我正在尝试从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_
#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);
}
#包括
#包括
#包括
int main(){
std::字符串url=”http://mydomain.com/randompage.php";
boost::regex exp(“^https?:/([^/]*?)/”;
std::cout您需要使用带有匹配结果对象的正则表达式搜索重载。在您的情况下:
#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);
}
}
#包括
#包括
#包括
int main(){
std::字符串url=”http://mydomain.com/randompage.php";
boost::regex exp(“^https?:/([^/]*?)/”;
小比赛;
if(boost::regex_搜索(url、match、exp))
{
std::cout first,second我编译此文件时出错。main.cpp:在函数'int main()':main.cpp:11:error:'const struct boost::sub_match'没有名为'begin'main的成员。cpp:11:error:'const struct boost::sub_match'没有名为'end'的成员我的错误。我将修复它。它应该是第一个和第二个,而不是begin()和end()。