正则表达式替换为c ++ 11中的回调?

rsk*_*k82 18 c++ regex callback c++11

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

我尝试过这种方法,但显然不起作用:

cout << regex_replace("my values are 9, 19", regex("\d+"), my_callback);
Run Code Online (Sandbox Code Playgroud)

和功能:

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

结果应该是: my values are 10, 20

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

我的意思是最新的4.9 gcc,能够正常运行而无需提升.

小智 21

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

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

  • +1 解决方案,但您需要使用除“std”以外的命名空间。目前,您的示例具有 [未定义行为](https://timsong-cpp.github.io/cppwp/n3337/namespace.std),因为您正在_重载_`std::regex_replace`,而不是_专门化_它。 (2认同)

Ben*_*ley 13

你可以用一个 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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,{-1,0}迭代器构造函数的参数列表中是一个列表,指定了我们要迭代的子匹配.的-1是用于非匹配部分,并且0是用于第一子匹配.

另请注意,我没有广泛使用c ++ 11正则表达式功能,也不是专家.因此,此代码可能存在问题.但是对于您的具体输入,我测试了它,它似乎产生了预期的结果.如果您发现任何输入设置不起作用,请告诉我.