如何将提升regex_replace与 lambda 函数一起使用
How to use boost regex_replace with a lambda function?
我正在尝试使用 lambda 函数来调用std::string
类型的boost::regex_replace
。 我没有运气让所有类型都正确。
typedef boost::basic_regex<char> regex;
typedef boost::match_results<char> smatch;
std::string text = "some {test} data";
regex re( "\{([^\}]*)\}" );
text = boost::regex_replace( text, re, [&](smatch const & what) {
return what.str();
});
我使用的是typedef
而不是标准名称,因为我有几个地方使用 typedef'd/模板化字符类型而不是固定类型。
在此代码中,我收到此错误:/usr/include/boost/regex/v4/match_results.hpp:68:77: error: no type named 'difference_type' in 'struct boost::re_detail::regex_iterator_traits<char>'
BidiIterator>::difference_type difference_type;
如果你有一个符合 C++11 的编译器,你既不需要 Boost 也不需要 lambda。
要实现相同的目标,您可以使用std::regex
:
#include <iostream>
#include <regex>
int main()
{
std::string text = "some {test} data {asdf} more";
std::regex re("\{([^\}]*)\}");
std::string out;
std::string::const_iterator it = text.cbegin(), end = text.cend();
for (std::smatch match; std::regex_search(it, end, match, re); it = match[0].second)
{
out += match.prefix();
out += match.str(); // replace here
}
out.append(it, end);
std::cout << out << std::endl;
}
当然,对于简单的文本替换,您可以只使用std::regex_replace()
但它不能接受函子,只能接受静态格式字符串,可以选择使用组占位符:
std::string text = "some {test} data {asdf} more";
std::regex re("\{([^\}]*)\}");
std::string out = std::regex_replace(text, re, "<$1>");
如match_results参考页上所述,要boost::match_results
的第一个类型参数是双向迭代器类型;因此,例如,标准 typedef boost::smatch
是 match_results<std::string::const_iterator>
。
要修复代码,您需要更正 typedef smatch
并删除 lambda 参数what
上的引用或使其成为常量引用:
typedef boost::basic_regex<char> regex;
typedef boost::match_results<std::string::const_iterator> smatch;
std::string text = "some {test} data";
regex re("\{([^\}]*)\}");
text = boost::regex_replace(text, re, [] (const smatch& what) {
return what.str();
});
smatch
类型有问题。我找不到带有类型名的工作示例,但是使用 C++14 自动 lambda 参数解决了这个问题:
auto text = "some {test} data";
regex re( "\{([^\}]*)\}" );
text = boost::regex_replace( text, re, [&](auto & what) {
return what.str();
});
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 两组使用lambda函数的大括号
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 为什么我不能在 constexpr lambda 函数中使用 std::tuple
- C++:Lambda 函数指针转换的用例是什么?
- 将 lambda 函数作为参数传递C++
- 如何将 lambda 函数作为参数发送到另一个函数
- 传递 lambda 函数的权衡是什么?
- 如何使用类模拟 C++11 中的 lambda 函数和闭包?
- 不是 lambda 函数中的常量表达式
- 如何使用可变参数数重载 lambda 函数?
- 如何通过指针传递lambda函数?
- 使用带有 lambda 函数指针的模板
- openmp c++ 中并行块内 lambda 函数的奇怪行为
- C++ 中 Lambda 函数中的溢出
- 将数组传递到 lambda 函数中
- lambda 函数未显示正确的结果