繁体   English   中英

C ++ 14 regex_search 0匹配

[英]C++14 regex_search 0 matches

我试图在我使用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绑定到临时值。 str()返回的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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM