【问题标题】:std::regex_replace: access index of disjunct match groupsstd::regex_replace:分离匹配组的访问索引
【发布时间】:2017-08-28 04:03:56
【问题描述】:

我的任务是用 std::regex 替换 boost::regex,并遇到了 boost::regex 引用以捕获组索引的问题。

正则表达式如下所示:

re = "(^Match1$)|(^Match2$)|(^Match3$)|(^Match4$)"

使用替换字符串:

replace_str = "(?{1}M1)(?{2}M2)(?{3}M3)(?{4}M4)"

如果我打电话给boost::regex_replace("Match1", re, replace_str, format_all) 我得到“M1”作为结果,std::regex_replace("Match1", re, replace_str) 结果由整个 replace_str 组成。

正则表达式的标准化变体没有 format_all 选项(不确定这是否是问题所在)。 http://en.cppreference.com/w/cpp/regex/match_flag_type

如何使用 c++11/14 正则表达式库重现类似的行为? 我是否错过了允许在没有提升的情况下使用该行为的选项或语法?

【问题讨论】:

    标签: c++ regex c++11 boost


    【解决方案1】:

    std::regex library 不支持 Boost 正则表达式引擎支持的 Boost-Extended Format String Syntax

    如果您正在寻找 Boost 的替代方案,您可以考虑切换到支持类似条件替换模式语法的 PCRE2。请参阅 PCRE2 Extended Replacement String Syntax 文章。

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多