首页 > 解决方案 > C++ std::regex 不支持前瞻反向引用

问题描述

我想匹配一个规则,一些名字像ABA一样出现,并且A不能等于B。

例如,“allen_bob_allen”是有效的。'allen_allen_allen' 无效。在https://regexr.com/,我编写了正则表达式模式 ([a-z]+)_(?!\1)([a-z]+)_\1,它运行良好,

但是当我用 C++ 编写这个时,使用 std::regex,

using namespace std;
#include <regex>
int main() {
    std::regex regex_test2("([a-z]+)_(?<!\\1)([a-z]+)_\\1");
    return 0;
}

我收到以下错误:

libc++abi.dylib: terminating with uncaught exception of type std::__1::regex_error: The expression contained an invalid back reference.

这是我的编译器信息:

在此处输入图像描述 有没有什么办法解决这一问题?

标签: c++regexbackreference

解决方案


推荐阅读