包括

#include <fstream>
#include <string>
#include<string>
#include<boost/algorithm/string.hpp>
#include<boost/regex.hpp>
#include <boost/algorithm/string/trim.hpp>
using namespace std;
using namespace boost;


int main() {
    string robotsfile="User-Agent: *"
            "Disallow: /";

    regex exrp( "^Disallow:(.*)$");

            match_results<string::const_iterator> what;

            if( regex_search( robotsfile, what, exrp ) )

            {

                string s( what[1].first, what[1].second );


                cout<< s;
            }

    return 0;
}

我需要走这条路 /Disallow: / 我的正则是什么问题?

有帮助吗?

解决方案

string robotsfile = "User-Agent: *"
    "Disallow: /";

上面的字符串文字被合并为“用户代理: *禁止: /”,您可能没有想到。由于您的正则表达式指出字符串必须以“禁止”单词开头,因此它不匹配。逻辑上正确的代码将是这样的:

string robotsfile = "User-Agent: *\n"
    "Disallow: /";

或者

string robotsfile = "User-Agent: *\nDisallow: /";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top