有没有办法将不连续的捕获组组合到单个正则表达式中?

Is there a way to combine discontinuous capture groups in a single regex?

本文关键字:组合 单个 正则表达式 不连续 有没有      更新时间:2023-10-16

我正在使用C++ RE2。我想将串联的不连续捕获组保存在结果字符串中。 正则表达式本身可能有一个或多个捕获组。

RE2::PartialMatch(sourceStr, Regex, &result)

sourceStr = "This is an example."
Regex = "(This).*(example)"
result = "Thisexample"

我怎样才能做到这一点?

你可以试试:

string result = sourceStr;
RE2::Replace(&result, "(This).*(example)", "$1$2");

当工作室本身已经存在库时,为什么要使用外部库。将其与"匹配功能"一起使用。 进一步学习的参考链接:https://en.cppreference.com/w/cpp/regex