c++ - regex replace with callback in c++11? -


is there function of regex replacement send matches user function , substitute return value:

i've tried method, doesn't work:

cout << regex_replace("my values 9, 19", regex("\d+"), my_callback); 

and function:

std::string my_callback(std::string &m) {   int int_m = atoi(m.c_str());   return std::to_string(int_m + 1); } 

and result should be: my values 10, 20

i mean similar mode of working php's preg_replace_callback or python's re.sub(pattern, callback, subject)

and mean latest 4.9 gcc, capable of regex without boost.

i wanted kind of function , didn't answer "use boost". problem benjamin's answer provides tokens. means don't know token match , doesn't let use capture groups. does:

// clang++ -std=c++11 -stdlib=libc++ -o test test.cpp #include <cstdlib> #include <iostream> #include <string> #include <regex>  namespace std {  template<class bidirit, class traits, class chart, class unaryfunction> std::basic_string<chart> regex_replace(bidirit first, bidirit last,     const std::basic_regex<chart,traits>& re, unaryfunction f) {     std::basic_string<chart> s;      typename std::match_results<bidirit>::difference_type         positionoflastmatch = 0;     auto endoflastmatch = first;      auto callback = [&](const std::match_results<bidirit>& match)     {         auto positionofthismatch = match.position(0);         auto diff = positionofthismatch - positionoflastmatch;          auto startofthismatch = endoflastmatch;         std::advance(startofthismatch, diff);          s.append(endoflastmatch, startofthismatch);         s.append(f(match));          auto lengthofmatch = match.length(0);          positionoflastmatch = positionofthismatch + lengthofmatch;          endoflastmatch = startofthismatch;         std::advance(endoflastmatch, lengthofmatch);     };      std::sregex_iterator begin(first, last, re), end;     std::for_each(begin, end, callback);      s.append(endoflastmatch, last);      return s; }  template<class traits, class chart, class unaryfunction> std::string regex_replace(const std::string& s,     const std::basic_regex<chart,traits>& re, unaryfunction f) {     return regex_replace(s.cbegin(), s.cend(), re, f); }  } // namespace std  using namespace std;  std::string my_callback(const std::smatch& m) {   int int_m = atoi(m.str(0).c_str());   return std::to_string(int_m + 1); }  int main(int argc, char *argv[]) {     cout << regex_replace("my values 9, 19", regex("\\d+"),         my_callback) << endl;      cout << regex_replace("my values 9, 19", regex("\\d+"),         [](const std::smatch& m){             int int_m = atoi(m.str(0).c_str());             return std::to_string(int_m + 1);         }     ) << endl;      return 0; } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -