首页 文章

boost :: regex_replace中两个参数格式函数的问题

提问于
浏览
7

我在 boost::regex_replace 中遇到格式化功能问题 . 我可以调用它的一个参数版本,但不能调用两个参数:

e = "(^|>)([^<>]+)";
h_str = regex_replace(h_str, e, repl_quot, boost::match_default);

其中 repl_quot 定义为

std::string const &repl_quot(boost::smatch const &what) {
    boost::regex e("\"");
    std::string  repl("&#34;");
    static std::string;
    str = regex_replace(what[0].str(), e, repl, boost::match_default);
    return str;
}

上面的工作,但我真的不想使用那个静态变量,所以我尝试了我认为是一个可接受的两个参数替代版本:

std::string const &repl_quot2(boost::smatch const &what, std::string &out) {
    boost::regex e("\"");
    std::string  repl("&#34;");
    out = regex_replace(what[0].str(), e, repl, boost::match_default);
    return out;
}

但是regex_replace不会接受这个(一个错综复杂的编译器错误) . 我正在尝试使用基于 Boost::Regex 文档中的以下两个参数版本:

template basic_string regex_replace(const basic_string&s,const basic_regex&e,Formatter fmt,match_flag_type flags = match_default);要求类型Formatter必须是......一元,二元或三元函子,它从函数调用计算替换字符串:fmt(what)必须返回char_type的容器以用作替换文本,或者fmt (what,out)或fmt(what,out,flags),两者都将替换文本写入* out,然后返回新的OutputIterator位置 . 在每种情况下,match_results对象是什么,表示找到的匹配项 .

已经多次请求编译器错误消息,所以这里(小心你要求的):

c:\ boost \ boost \ regex \ v4 \ regex_format.hpp在成员函数OutputIter boost :: re_detail :: format_functor_container :: operator()(const Match&,OutputIter,boost :: regex_constants :: match_flag_type,const Traits&)[使用OutputIter = boost :: re_detail :: string_out_iterator,std :: allocator >>,Container = const std :: string&(*)(const boost :: smatch&,std :: string&),Match = boost :: match_results <__ gnu_cxx: :__ normal_iterator,std :: allocator >>,std :: allocator,std :: allocator >>>>,Traits = boost :: regex_traits_wrapper >>]':356 c:\ boost \ boost \ regex \ v4 \ match_results . hpp实例化为OutputIterator boost :: match_results :: format(OutputIterator,Functor,boost :: regex_constants :: match_flag_type,const RegexT&)const [with OutputIterator = boost :: re_detail :: string_out_iterator,std :: allocator >>,Functor = const std :: string&(*)(const boost :: smatch&,std :: string&),RegexT = boost :: basic_regex >>,BidiIterator = __gnu_cxx :: __ normal_iterator,std :: allocator >>,Allocator = std :: allocator ,圣d :: allocator >>>>]'60 c:\ boost \ boost \ regex \ v4 \ regex_replace.hpp从OutputIterator boost :: regex_replace实例化(OutputIterator,BidirectionalIterator,BidirectionalIterator,const boost :: basic_regex&,Formatter,boost: :regex_constants :: match_flag_type)[与OutputIterator = boost :: re_detail :: string_out_iterator,std :: allocator >>,BidirectionalIterator = __gnu_cxx :: __ normal_iterator,std :: allocator >>,traits = boost :: regex_traits>,charT = char ,Formatter = const std :: string&(*)(const boost :: smatch&,std :: string&)]'80 c:\ boost \ boost \ regex \ v4 \ regex_replace.hpp从std :: basic_string,std实例化: :allocator <_T2 >> boost :: regex_replace(const std :: basic_string,std :: allocator <_T2 >> &&,const boost :: basic_regex&,Formatter,boost :: regex_constants :: match_flag_type)[with traits = boost :: regex_traits>,charT = char,Formatter = const std :: string&(*)(const boost :: smatch&,std :: string&)]'327 C:\ Dev-Cpp \ Examples \ wordrad \ xhtml_open.cpp from here here 1064 C:\提升\ boost \ regex \ v4 \ regex_format.hpp请求成员begin'in((boost :: re_detail :: format_functor_container,std :: allocator >>,std :: allocator,std :: allocator >>>>>,boost :: regex_traits_wrapper >>> )this) - > boost :: re_detail :: format_functor_container,std :: allocator >>,std :: allocator,std :: allocator >>>>>,boost :: regex_traits_wrapper >>> :: func ',这是非类型的const std :: string&(* const)(const boost :: smatch&,std :: string&)'1064 c:\ boost \ boost \ regex \ v4 \ regex_format.hpp会员请求结束'((boost :: re_detail :: format_functor_container,std :: allocator >>,std :: allocator,std :: allocator >>>>,boost :: regex_traits_wrapper >>> *)this) - > boost: :re_detail :: format_functor_container,std :: allocator >>,std :: allocator,std :: allocator >>>>,boost :: regex_traits_wrapper >>> :: func',它是非类型的const std :: string&( const)(const boost :: smatch&,std :: string&)'

1 回答

  • 1

    好的,这是我写repl_quot2的方式:

    struct formatter
    {       
      template<typename Out>
      Out operator()(boost::smatch const &what, Out out) const {
        boost::regex e("\"");    
        std::string  repl("&#34;");
        std::string str
          = regex_replace(what[0].str(), e, repl, boost::match_default);
        out = std::copy(str.begin(), str.end(), out);
        return out;
      }
    
    };
    

    然后从regex_replace调用它时:

    e = "(^|>)[^<>]+";
      formatter repl_quot2;
      h_str = regex_replace(h_str, e, repl_quot2, boost::match_default);
    

    这对应于http://boost-sandbox.sourceforge.net/libs/xpressive/doc/html/boost_xpressive/user_s_guide/string_substitutions.html处的文档 .

    令我困惑的是,如果调用了两个参数版本,它需要一个仿函数(一个带()运算符的类而不是函数,但是对于一个参数版本不需要仿函数(OP中的repl_quot) ) . 无论如何,还没有得到新的两个parm版本作为一个直接的功能 . 但主要的问题是 out ,我必须传递并作为模板参数返回,如图所示,而不是像在OP中那样使它成为std :: string . 它应该是一个OutputIterator - 还不知道究竟是什么 .

    顺便说一句,所有这个正则表达式都是替换带有html实体版本的双引号,“在html中的任何文本中都不是标记的一部分 .

    另外,我想替换原始函数repl_quot的原因是我必须将返回值存储在repl_quot中的静态局部变量中 . 它只能返回一个普通的局部变量,因为它甚至可以在被使用之前被解除分配(并导致崩溃) . repl_quot正在工作 - 我的静态问题是它不是线程安全的,并且不知道Boost :: RegEx是否是多线程的 . 我想我把它编译为多线程,但静态var似乎没有引起问题 . 但是使用repl_quot2我将返回值写入输出参数 .

相关问题