- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试匹配我使用 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 © 2009-. "Minecraft" is a trademark of Mojang Synergies AB — <a href="/terms">Terms of Use
</a> — <a href="https://account.mojang.com/terms#privacy">Privacy Policy</a> — 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/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!