c++ - 这个boost c++正则表达式代码有什么问题?

标签 c++ regex string-concatenation string-literals boost-regex

包括

#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: /";

上面的字符串文字被合并到“User-Agent: *Disallow:/”中,并且没有你想象的换行符。由于您的正则表达式声明字符串必须以“Disallow”字样开头,因此它不匹配。逻辑上正确的代码应该是这样的:

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

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

关于c++ - 这个boost c++正则表达式代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3751387/

相关文章:

c++ - 在 Rcpp 中包装一个 Fortran 函数

regex - 如何使用 Perl 进行基于计算的替换?

r - gsub中的正则表达式问题

c++ - 使用 Eigen 的插件在第二次运行时崩溃

c++ - 如何将 char 转换为关联的转义序列?

Ruby TCPSocket 读取直到自定义终止符

java - 为什么在返回值之前将字符串与空值连接起来?

php: output[] w/join vs $output .=

c++ - Eclipse CDT 生成的 getters/setters 名称

regex - sed 匹配大括号之间的匹配