问题
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