gpt4 book ai didi

C++14 regex_search 0 匹配

转载 作者:太空狗 更新时间:2023-10-29 21:16:40 24 4
gpt4 key购买 nike

我正在尝试匹配我使用 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;
}

打印响应时我得到的响应是这样的:

<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<title>Minecraft</title>
<meta name="description" content="Minecraft is a game about placing blocks to build anything you can imagine. At
night monsters come out, make sure to build a shelter before that happens.">
<meta name="author" content="Mojang AB">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link rel="shortcut icon" href="/favicon.png">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="stylesheet" href="/stylesheets/style.css?b=b_965">
<link rel="stylesheet" media="handheld" href="/stylesheets/handheld.css?b=b_965">

<!--[if !IE 7]>
<style type="text/css">
#wrap {display:table;height:100%}
</style>
<![endif]-->
<script src="/javascripts/libs/modernizr.min.js"></script>


<script>
function recordOutboundLink(link, category, action, label, value) {
try {
ga('send', 'event', category, action, label, value);
setTimeout('document.location = "' + link.href + '"', 100);
}catch(err){}
}

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-9482675-1', 'auto');
ga('send', 'pageview');


</script>
</head>
<body>
<div id="wrap">
<header>
<div id="header_container" class="clearfix">
<a id="logo" href="/">Minecraft</a>
<ul id="menu">
<li><a href="/">Home</a></li>
<li>
<a href="/game">Game</a>
<ul>
<li><a href="/game">What is Minecraft?</a></li>
<li><a href="/game/howtoplay">Getting started</a></li>
<li><a href="/game/credits">Credits</a></li>
</ul>
</li>
<li><a href="/community">Community</a></li>
<li><a href="/store">Store</a></li>
<li><a href="/profile">Profile</a></li>
<li><a href="https://help.mojang.com">Help</a></li>
</ul>
<div id="userbox">
<span class="logged-out"><a href="/login" >Log i
n</a> | <a href="/register" onclick="recordOutboundLink(this, 'Sales2', 'Purchase-interest', 'Register-link')">Register<
/a></span>
</div>
</div>
</header>

<noscript>
<div id="javascript-warning" class="warning warning-yellow">
Please, please enable JavaScript to use this site.
</div>
</noscript>

<div id="main" role="main" class="clearfix controller-Secure action-login">


<h1>Login</h1>


<div id="login">
<h1></h1>
<form action="https://minecraft.net/login" method="post" accept-charset="utf-8" enctype="application/x-www-form-urle
ncoded" id="loginForm" class="spacious" id="loginForm" class="spacious"><input type="hidden" name="authenticityToken" v
alue="d2edcaa6bcc0fb19bf299b381212f59a194b8884">



<p>When logging in with a Mojang account, use your e-mail address as username.</p>

<p id="username-field">
<label for="username">Username:</label>
<input tabindex="1" type="text" name="username" id="username" value="" />
<a href="/retrievename">Forgot username?</a>
</p>
<p id="password-field">
<label for="password">Password:</label>
<input tabindex="2" type="password" name="password" id="password" value="" />
<a href="/resetpassword">Forgot password?</a>
</p>
<p id="remember-field">
<input type="checkbox" name="remember" id="remember" value="true" />
<label for="remember">Remember me</label>
</p>
<p id="signin-field">
<input type="submit" id="signin" value="Sign in" />
</p>

</form></div>
</div>
</div>

<footer>
Mojang &copy; 2009-. "Minecraft" is a trademark of Mojang Synergies AB &mdash; <a href="/terms">Terms of Use
</a> &mdash; <a href="https://account.mojang.com/terms#privacy">Privacy Policy</a> &mdash; b_965 r_bb50ffaf2f187302eb1bb
937f03df44f30b7d465
</footer>

<script src="/javascripts/libs/json2.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="/javascripts/libs/jquery-1.5.1.min.js"%3E%3C/s
cript%3E'))</script>
<script src="/javascripts/libs/jquery.dataTables.min.js"></script>
<script src="/javascripts/libs/jquery.timeago.js"></script>
<script src="/javascripts/jquery.scrollTo-1.4.2-min.js?b=b_965"></script>
<script src="/javascripts/plugins.js?b=b_965"></script>
<script src="/javascripts/main.js?b=b_965"></script>
</body>
</html>

当我运行这段代码时,我总是得到“没有找到合适的值!”输出。但是如果我通过 regexr 运行相同的正则表达式我一点问题都没有,它设法找到了一场比赛。在这种情况下,我正在寻找的匹配项是:

alue="d2edcaa6bcc0fb19bf299b381212f59a194b8884

我在这里做错了什么?

最佳答案

const char* responseArray = response.str().c_str(); — 将 responseArray 绑定(bind)到临时值。 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;
}

关于C++14 regex_search 0 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34529145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com