c++11中的Regex替换为回调
regex replace with callback in c++11?
是否有一个正则表达式替换函数,将匹配发送给用户函数,然后替换返回值:
我试过这个方法,但它显然不起作用:
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::ostringstream
比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();
}
所以,你可以看到我用std::sregex_iterator
代替std::sregex_token_iterator
这种功能只存在于regex_replace的Boost库版本中,它可以具有自定义格式化器。不幸的是,标准c++ 11实现要求替换格式参数必须是一个字符串。
以下是regex_replace的文档:http://www.cplusplus.com/reference/regex/match_replace/
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 用于在回调中调用解析器的设计模式
- 如何使用C++对象的成员函数作为 C 样式回调?
- Java从C++回调到C++回调
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 不带轮询的 SDL2 事件回调
- C++存储带有可变参数的回调
- 如何使用 Node-addon-API 实现 node-nan 回调
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- 访问类C++ C 样式回调
- 用回调函数替换C++正则表达式
- 用于替换回调命名空间的模板?
- 我如何用在这个回调中具有编译时类型检查的东西替换void指针?
- c++11中的Regex替换为回调