regex replace with callback in c++11?

c regex
std::regex
c regex library
c++ regex example
c++ regex tokenizer
c regex iterator
regex c 17
regex cppref

Is there a function of regex replacement that will send the matches to user function and then substitute the return value:

I've tried this method, but it obviously doesn't work:

cout << regex_replace("my values are 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 the result should be: my values are 10, 20

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

And I mean the latest 4.9 gcc, that is capable of regex without boost.

I wanted this kind of function and didn't like the answer "use boost". The problem with Benjamin's answer is it provides all the tokens. This means you don't know which token is a match and it doesn't let you use capture groups. This 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::regex_iterator<BidirIt> 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 are 9, 19", regex("\\d+"),
        my_callback) << endl;

    cout << regex_replace("my values are 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;
}

regex replace with callback in c++11? - c++ - android, Is there a function of regex replacement that will send the matches to user function and then substitute the return value: I've tried this method, but it obviously� Conditional Regex Replacement in Text Editor Often, the need arises to replace matches with different strings depending on the match itself. For instance, let's say you want to match words for colors, such as red and blue , and replace them with their French equivalents— rouge and bleu .

You could use a regex_token_iterator

#include <iostream>
#include <algorithm>
#include <regex>
#include <string>
#include <sstream>

int main()
{
    std::string input_text = "my values are 9, 19";
    std::string output_text;
    auto callback = [&](std::string const& m){
        std::istringstream iss(m);
        int n;
        if(iss >> n)
        {
            output_text += std::to_string(n+1);
        }
        else
        {
            output_text += m;
        }
    };

    std::regex re("\\d+");
    std::sregex_token_iterator
        begin(input_text.begin(), input_text.end(), re, {-1,0}),
        end;
    std::for_each(begin,end,callback);

    std::cout << output_text;
}

Note that the {-1,0} in the argument list of the iterator constructor is a list specifying the submatches we want to iterate over. The -1 is for non-matching sections, and the 0 is for the first submatch.

Also note that I have not used the c++11 regex functionality extensively and am no expert in it. So there may be problems with this code. But for your specific input, I tested it and it seems to produce the expected results. If you find any input set for which it doesn't work, please let me know.

preg_replace_callback - Manual, preg_replace_callback — Perform a regular expression search and replace using You'll often need the callback function for a preg_replace_callback() in just� Makes a copy of the target sequence (the subject) with all matches of the regular expression rgx (the pattern) replaced by fmt (the replacement). The target sequence is either s or the character sequence between first and last, depending on the version used.

Maybe I arrived too late to this party (about 5 years thought), but I neither liked the answer "use boost", following function has less generalization (speaking about string types), but apparently works. However, I don't know if use a std::ostringstream is better than std::string::append:

std::string regex_replace(
    const std::string& input,
    const std::regex& regex, 
    std::function<std::string(std::smatch const& match)> format) {

    std::ostringstream output;
    std::sregex_iterator begin(input.begin(), input.end(), regex), end;
    for(; begin != end; begin++){
        output << begin->prefix() << format(*begin);
    }
    output << input.substr(input.size() - begin->position());
    return output.str();
}

So, as you can see I used std::sregex_iterator instead of std::sregex_token_iterator.

String.prototype.replace(), A RegExp object or literal. The match or matches are replaced with newSubstr or the value returned by the specified function . substr� Pattern: We use a pattern to replace all 3-letter sequences starting and ending with certain letters with a replacement string. Tip: The Regex method allows you to replace variations in the string in one statement. Parameters: Regex.Replace is a public static method and we pass it 3 parameters—the input, the pattern and the replacement string

That kind of functionality only exists in the Boost library version of regex_replace, which can have the custom formatter. Unfortunately, the standard C++11 implementation requires the replacement format argument must be a string.

Here is the documentation on regex_replace: http://www.cplusplus.com/reference/regex/match_replace/

Perform a regular expression search and replace using a callback, The behavior of this function is almost identical to preg_replace(), except for the fact that instead of replacement parameter, one should specify a callback . Regular expressions (regex or regexp) even during a code session using a common IDE to translate a Java or C# class in the respective JSON object — replace “;” with “,” make it

PHP, php documentation: String replace with callback. then she said 789hij"; $regex = "/\b(\d+)\w+/"; // This function replaces the matched entries conditionally� If you are adding usefull regex functions at the end of the code, you could also add information on replace function taking a callback as an argument. const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?'; p.replace('dog', (match, matchIndex, originalString) =>

TIL: JavaScript replace() command with callback - DEV, The replace() command in JavaScript has a callback that provided you with some more information How would you write a regex for that? Technically a " callback function" is one that's called at a later point in time, like� Function RegExpReplace (re, str, replacement) ' If replacement is a string, use the native RegExp.Replace function. If TypeName (replacement) = "String" Then. RegExpReplace = re.Replace (str, replacement) ' Since replacement is not a string, call replacement with every match. ' object and replace the match with the return value.

preg_replace_callback, preg_replace_callback — Perform a regular expression search and replace using You'll often need the callback function for a preg_replace_callback() in just� C++ Regex Library - regex_replace - It makes a copy of the target sequence (the subject) with all matches of the regular expression rgx (the pattern) replaced by fmt (the replacement).

Comments
  • +1 for the solution, but you need to use a namespace other than std. Currently your example has undefined behavior, since you're overloading std::regex_replace, not specializing it.
  • @andree is correct - this solution is helpful assuming you never plan to use std::regex_replace with string replacements; otherwise, the compiler will throw an error due to ambiguity.
  • It works. But I think Boost is a better solution. stackoverflow.com/questions/11508798/…
  • I'm not too up to date with C++ standards but is this std::function<std::string(std::smatch const& match)> a function def that can be used elsewhere, like whatever the format points to ?
  • I been writing c++ (old standard) for a long time and I can't stand to see block braces on the same line as code... just me!
  • Hi zatarain, a nice idea, but it hasn't worked out of the box. It seems to make problems, if the match is at the beginning of the string. I've tried to add a separate index integer, which is updated inside the for loop as the last statement: subStrStartIndex = begin->position() + begin->length(). Than you can use it for the output stream in the for loop: output << input.substr(subStrStartIndex, begin->position() - subStrStartIndex) << format(*begin); And you avoid to derefence begin outside of the for loop, which shouldn't be valid anymore: output << input.substr(subStrStartIndex);