我正在尝试从 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);
}
如何打印匹配的值?
最佳答案
您需要使用带有 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);
}
}
编辑:更正开始,结束==>第一,第二
关于c++ - 如何使用 Boost.Regex 获取正则表达式匹配值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3074290/