根据 regex101.com 和我称为“RegExRx”的应用程序,这是一个有效的正则表达式
(?<=\().*
也就是说,这应该匹配左括号字符后面的所有内容。以下是 regex101.com 对此的分析
/(?<=()./ (?<=() Positive Lookbehind - Assert that the regex below can be matched ( matches the character ( literally . matches any character (except newline) Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
但是,这个 C++11 程序会抛出异常
libc++abi.dylib: terminating with uncaught exception of type std::__1::regex_error: The expression contained mismatched ( and ).
这是 Xcode 5.1.1 附带的 Clang。
问题:Clang 应该接受这个正则表达式吗?如何获得一个在语义上与此等效的 std::regex?
#include <iostream>
#include <regex>
int main(int argc, const char * argv[])
{
std::string x{ "(?<=\\().*" };
std::cout << "Here is my regex string " << x << std::endl;
std::regex{ x }; // throws
return 0;
}
编辑:我的问题与建议的重复问题不同,因为我问“如何获得一个在语义上与此等效的 std::regex?”下面的用户 hwnd 提供了语义上等效的解决方法,非常有帮助。
最佳答案
C++11使用ECMAScript的正则表达式语法,不支持lookbehind。
与上述正则表达式等效的内容如下 -
\\((.*)
注意:捕获组( ... )
保留左括号后面的所有内容。
关于C++ Clang 无法解析此 Lookbehind 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189944/