c++ - 如何从 C++ 中的 curl 输出中搜索所有出现的正则表达式?

标签 c++ regex boost

我想在 curl 输出中搜索每个 IP 地址。有快速的方法吗?我从 boost 了解到 regex_search,但据我了解,它是针对文件的。

我的实际非工作代码:

#include <iostream>
#include <curl/curl.h>
#include <boost/regex.hpp>

using namespace std;

boost::regex expression("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
boost::smatch what;  // "match" specialized for std::string of char
boost::match_flag_type flags = boost::match_default;

string buffer = "hey";

int writer(char *data, size_t size, size_t nmemb, string *buffer){
    int result = 0;
    if(buffer != NULL) {
        buffer -> append(data, size * nmemb);
        result = size * nmemb;
    }
    return result;
} 

int main(int argc, char *argv[]) {
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();

    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.xroxy.com/proxylist.php");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0); /* Don't follow anything else than the particular url requested*/
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); /* Function Pointer "writer" manages the required buffer size */
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer ); /* Data Pointer &buffer stores downloaded web content */       
        curl_easy_perform(curl);
        /* always cleanup */ 
        curl_easy_cleanup(curl);
    }

    if (boost::regex_search(buffer.begin(), buffer.end(), what, expression, flags) ) {
        cout << "found: " << what << endl;
    }
    return 0;
}

最佳答案

boost::regex 可用于搜索字符串。

我不知道你如何获取curl的输出,但我想你可以将它放入一个std::string,然后你可以用boost搜索它。

std::string s(/*...*/);  
boost::regex expression("[abc]{5}"); // just an example
boost::smatch what;  // "match" specialized for std::string of char
boost::match_flag_type flags = boost::match_default;

if ( boost::regex_search(s.begin(), s.end(), what, expression, flags) ) {
   cout << "found: " << what << endl;
}

boost::smatch 是一个非常通用且有用的类,它可以为您提供作为 std::string 的整个匹配、开始和结束的迭代器,以及中的每个子组你的正则表达式

关于c++ - 如何从 C++ 中的 curl 输出中搜索所有出现的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4601742/

相关文章:

c++ - 语义 Action 在 boost::spirit 解析中运行多次

c++ - 在 Eclipse 中将 Boost 链接到我的 C++ 项目

c++ - 程序地址范围

c++ - fatal error : string. h:没有那个文件或目录

php - 从 MySQL INSERT 查询字符串中提取括号内的值

python - Python 3.2 中的部分正则表达式匹配

c++ - Boost.Program_options 在 Clang 下没有正确链接

c++ - 用于稍后引用的 lambda 和函数的通用类型转换

c++ - Python C API PyObject_Repr 段错误(核心转储)

c# - 从多行中解析行组