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

Mar*_*ark 7 c++ regex templates boost-regex

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

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

在哪里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;
}
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

但是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&)[with 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,std :: 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)[with 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 ::串&)]'

327 C:\ Dev-Cpp\Examples\wordrad\xhtml_open.cpp从这里实例化

1064 c:\ boost\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请求成员end' 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&)'

Mar*_*ark 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;
  }

};
Run Code Online (Sandbox Code Playgroud)

然后从 regex_replace 调用它时:

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

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

目前令我困惑的是,如果调用两个参数版本,它需要一个函子(带有 () 运算符的类)而不是一个函数,但不需要单参数版本的函子(OP 中的 repl_quot )。不管怎样,还没有让新的两个参数版本作为直接函数工作。但主要问题是out我必须作为模板参数传递和返回,如图所示,而不是像OP中那样将其设置为std::string。它应该是一个 OutputIterator - 仍然不知道它实际上是什么。

顺便说一句,这个正则表达式所做的就是将双引号替换为 html 实体版本 \" 在 html 中不属于标签的任何文本中。

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