Как я могу найти все вхождения регулярного выражения из вывода curl в C ++? - PullRequest
1 голос
/ 05 января 2011

Я хотел бы найти каждый IP-адрес в выводе curl. Есть ли быстрый способ сделать это? Я знаю о regex_search от boost, но из того, что я прочитал, он предназначен для файлов.

Мой действительный нерабочий код:

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

Ответы [ 2 ]

1 голос
/ 05 января 2011

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, итераторы до его начала и конца, а также для каждой подгруппы в вашем регулярном выражении

0 голосов
/ 05 января 2011

regex_search из boost, но из того, что я прочитал, предназначено для файлов.

Ты уверен? Из того, что я вижу здесь: http://www.boost.org/doc/libs/1_45_0/libs/regex/doc/html/boost_regex/ref/regex_search.html

Determines whether there is some sub-sequence within [first,last) that matches the regular expression e, parameter flags is used to control how the expression is matched against the character sequence. Returns true if such a sequence exists, false otherwise.

Я что-то здесь упускаю?

В любом случае, другой вариант - фильтровать вывод curl через grep.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...