invalid back reference in lookahead in c++ std::regex

冷暖自知 提交于 2021-01-28 11:51:54

问题


It seems that c++ std::regex doesn't support back reference in lookahead. http://www.regular-expressions.info only says that c++ doesn't support lookbehind,foreward reference and nested reference. I also scanned c++14 standard and found nothing. Too little information about c++ std::regex on web.

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main(int argc, char **argv) {
    regex re("(a)xxx(?=\\1)");
    smatch matchs;
    string str {"axxxa"};
    if(regex_search(str,matchs,re)) {
        cout << "matched" << endl;
    }else{
        cout<<"not matched"<<endl;
    }
    return 0;
}

The code compiles, but with run error at:

regex re("(a)xxx(?=\\1)");

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

My gcc version:
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0

B.T.W. the same regex works in Nodejs:

var t = /(a)xxx(?=\1)/;
console.log(t.exec("axxxa"));

Thanks!

来源:https://stackoverflow.com/questions/50189107/invalid-back-reference-in-lookahead-in-c-stdregex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!