c++11中的Regex替换为回调

regex replace with callback in c++11?

本文关键字:回调 替换 Regex 中的 c++11      更新时间:2023-10-16

是否有一个正则表达式替换函数,将匹配发送给用户函数,然后替换返回值:

我试过这个方法,但它显然不起作用:

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

和功能:

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

,结果应为:my values are 10, 20

我的意思是类似于php的preg_replace_callback或python的re.sub(pattern, callback, subject)的工作模式

我指的是最新的4.9 gcc,它可以在没有boost的情况下使用regex

我想要这种功能,但不喜欢"使用boost"的答案。本杰明的答案的问题在于它提供了所有的代币。这意味着您不知道哪个令牌是匹配的,并且它不允许您使用捕获组。这样做:

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

注意,迭代器构造函数的实参列表中的{-1,0}是一个列表,指定要迭代的子匹配项。-1用于不匹配的部分,0用于第一个子匹配。

还请注意,我没有广泛使用c++11的正则表达式功能,也不是这方面的专家。所以这段代码可能有问题。但是对于你的具体输入,我测试了一下,它似乎产生了预期的结果。

如果你发现任何输入集不工作,请告诉我。

也许我来得太晚了(大约5年的想法),但我既不喜欢答案"使用boost",下面的函数有较少的泛化(谈论字符串类型),但显然是有效的。然而,我不知道是否使用std::ostringstreamstd::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();
}

所以,你可以看到我用std::sregex_iterator代替std::sregex_token_iterator

这种功能只存在于regex_replace的Boost库版本中,它可以具有自定义格式化器。不幸的是,标准c++ 11实现要求替换格式参数必须是一个字符串。

以下是regex_replace的文档:http://www.cplusplus.com/reference/regex/match_replace/