【问题标题】:boost regex formatter, how to use a custom function提升正则表达式格式化程序,如何使用自定义函数
【发布时间】:2012-02-19 22:16:36
【问题描述】:

那么在调用 boost::regex_replace 时如何调用自定义格式化函数?

我的代码如下:

template <typename T>
std::string fmt(boost::match_results<T> match) {
    auto str = match[1];
    if (str == ".") {
        return "\".\"";
    } else {
        return str;
    }
}
void __ConvertEscapeChar(std::string& action, std::string regex) {
    boost::regex re(regex);
    action = boost::regex_replace(action, re, &fmt, boost::regex_constants::match_all);
}

但是它显示错误,“无法推断 __fmt 的模板参数”。 - 那么 T 实际上是什么?

【问题讨论】:

  • 您的函数名无效。见here
  • @BenjaminLindley Meh 改变了......但这并没有真正改变任何东西......(现在真的需要一个新的 PREfix 来识别本地函数)。
  • 它将您的代码更改为有效的 C++,而不是您当前的错误。
  • 如果“本地函数”是指在当前编译单元(.cc/.o 文件)之外不应该看到的函数,则可以将其放在匿名命名空间中:namespace { ConvertEscapeChar(...) {...} } .

标签: c++ regex boost boost-regex


【解决方案1】:

除非您需要在 fmt 函数中灵活使用模板,因为某些原因在这里并不明显,否则请尝试以下操作:

std::string fmt(boost::smatch match)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多