Problems with the function of two parameters in boost :: regex_replace

I am having problems with the format function in boost::regex_replace . I can call it one parameter, but not two parameters:

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

Where repl_quot is defined as

 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; } 

The above works, but I really do not want to use this static variable, so I tried what I considered an acceptable alternative version of two parameters:

 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; } 

But regex_replace will not accept this (confusing compiler error). I am trying to use two versions of the parameters based on the following from the Boost::Regex documentation:

template basic_string regex_replace (Const basic_string & s, const basic_regex & , Formatter fmt, match_flag_type flags = match_default);

Required The Formatter type must be either ... a unary, binary or triple functor that computes the replacement string from the function call: either fmt (what), which should return the char_type's container for use as replacement text, or either fmt (what, out) or fmt (that, out, flags), both of which write replace the text with * out, and then return a new OutputIterator position. In each case, what is the match_results object that represents the match found.

The requests for the compiler error message were repeated, so here it is (be careful what you ask for):

c: \ boost \ boost \ regex \ v4 \ regex_format.hpp In the member function `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, created from `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, created from `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, created from `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 created here

1064 c: \ boost \ boost \ regex \ v4 \ regex_format.hpp request for the begin' in member ((boost :: re_detail :: format_functor_container, std :: allocator →, std :: allocator, std :: allocator → →>, boost :: regex_traits_wrapper →>)) → boost :: re_detail :: format_functor_container, std :: allocator →, std :: allocator, std :: allocator → →> boost :: regex_traits_wrapper →> :: func ', which refers to non-class class `const std :: string & (* const) (const boost :: smatch &, std :: string &) '

1064 c: \ boost \ boost \ regex \ v4 \ regex_format.hpp request for member end' in ((boost :: re_detail :: format_functor_container, std :: allocator →, std :: allocator, std :: allocator → →>, boost :: regex_traits_wrapper →>)) → boost :: re_detail :: format_functor_container, std :: allocator →, std :: allocator, std :: allocator → →> boost :: regex_traits_wrapper →> :: func ', which refers to non-class class `const std :: string & (* const) (const boost :: smatch &, std :: string &) '

+7
source share
1 answer

OK, as I had to write 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; } }; 

And then calling it from regex_replace:

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

This is consistent with the documentation http://boost-sandbox.sourceforge.net/libs/xpressive/doc/html/boost_xpressive/user_s_guide/string_substitutions.html .

What puzzles me at the moment is that it requires a functor (a class with an operator ()), not a function if two versions of parameters are called, but no functor is needed for versions of one parameter (repl_quot in the OP). In any case, they did not receive a new two parm versions to work as a direct function. But the main problem was that out , which I had to pass and return as a template parameter, as shown, as opposed to making it std :: string as in OP. It was supposed to be an OutputIterator, until you know what it really is.

By the way, all this regular expression replaces double quotes with the html entity version, \ & amp; # 34; in any text in html is not part of the tag.

Also, the reason I wanted to replace my original repl_quot function was because I had to store the return value in a static local variable in repl_quot. This does not work just to return a normal local variable, because it can be freed before it can be used (and caused a crash). Repl_quot worked - my problem with statics is that it is not thread safe and does not know how much Boost :: RegEx works. I think I compiled it as multithreading, but the static var did not seem to cause problems. But with repl_quot2, I write the return value to the output parameter.

+1
source

All Articles