Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;14正则表达式搜索0个匹配项_C++_Regex_Libcurl_C++14 - Fatal编程技术网

C++ C++;14正则表达式搜索0个匹配项

C++ C++;14正则表达式搜索0个匹配项,c++,regex,libcurl,c++14,C++,Regex,Libcurl,C++14,我正在尝试匹配我使用curl刮取的网站中的正则表达式,以下是我的代码: #include <regex> #include "curl_easy.h" #include "curl_form.h" int main(int argc, const char **argv) { std::ostringstream response; curl::curl_ios<std::ostringstream> writer (response); c

我正在尝试匹配我使用curl刮取的网站中的正则表达式,以下是我的代码:

#include <regex>
#include "curl_easy.h"
#include "curl_form.h"

int main(int argc, const char **argv) {

    std::ostringstream response;
    curl::curl_ios<std::ostringstream> writer (response);

    curl::curl_easy easy(writer);
    // Add some option to the curl_easy object.
    easy.add<CURLOPT_SSL_VERIFYHOST>(false);
    easy.add<CURLOPT_SSL_VERIFYPEER>(false);
    easy.add<CURLOPT_URL>("https://minecraft.net/login");
    easy.add<CURLOPT_FOLLOWLOCATION>(1L);
    try {
        // Execute the request.
        easy.perform();
    } catch (curl::curl_easy_exception error) {
        // If you want to get the entire error stack we can do:
        curl::curlcpp_traceback errors = error.get_traceback();
        // Otherwise we could print the stack like this:
        error.print_traceback();
        // Note that the printing the stack will erase it
    }
    std::regex pattern("alue=\"\\w{40}");

    const char* responseArray = response.str().c_str();

    std::cout << response.str();

    std::smatch match;

    if(std::regex_search(responseArray, pattern)){
        std::cout << "Found the proper value!" << std::endl;
    } else {
        std::cout << "Did not find the proper value" << std::endl;
    }

    return 0;
}

我在这里做错了什么?

const char*responseArray=response.str().c_str()-将
responseArray
绑定到临时值。
str()
返回的字符串在此表达式之后被销毁

更好的解决方案将结果保存在
std::string
中并使用它:

std::string responseArray = response.str();

std::cout << responseArray;

std::smatch match;

if(std::regex_search(responseArray, pattern)){
    std::cout << "Found the proper value!" << std::endl;
} else {
    std::cout << "Did not find the proper value" << std::endl;
}

std::string responseArray=response.str();

我真的想知道为什么投票失败。我用代码清楚地陈述了我的问题。我还说明了发生了什么以及我预期会发生什么。在你的例子中,v和value之间有一条断线。当我移除它时,regex工作了。@Revolver\u Ocelot是的,我还以为它也是。但是后来我尝试了
std::regex模式(“value=\“\\w{40}”);
没有v,仍然是0个匹配项。让我修改这个问题来说明这一点。谢谢。但是regex\u搜索只接受一个字符数组。所以我所做的是将响应保存到
const std::string responseString=response.str();
并相应地更改了我的if:
if(std::regex_search(responseString.c_str(),pattern))
提交了一个编辑,修复了问题顺便说一句:)@ CZAARKK99有超负荷的代码> STD::String ,所以如果你的标准库符合C++标准,那就不是必要的了。@ USER 2079303我没有做任何深入的研究,我的IDE抱怨,所以我发现了一个快速修复。<代码> STD::String ActhSurray=响应。
就足够了,对吗?@AndyG是的,谢谢。我在编辑原始代码时错过了这一点。
alue="d2edcaa6bcc0fb19bf299b381212f59a194b8884
std::string responseArray = response.str();

std::cout << responseArray;

std::smatch match;

if(std::regex_search(responseArray, pattern)){
    std::cout << "Found the proper value!" << std::endl;
} else {
    std::cout << "Did not find the proper value" << std::endl;
}