C++ Clang 无法解析此 Lookbehind 正则表达式

标签 c++ regex c++11 clang

根据 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。

与上述正则表达式等效的内容如下 -

\\((.*)

注意:捕获组( ... ) 保留左括号后面的所有内容。

Working Demo

关于C++ Clang 无法解析此 Lookbehind 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189944/

相关文章:

c++ - 如何在另一个函数中访问在一个函数中定义和声明的数组?

c++ - MySQL Connector C++ cgi 程序是否像 php 一样易受 MySQL 注入(inject)攻击?

c++ - 允许用户包含类而不包含用于私有(private)的 header

c++ - 类的锯齿状数组

c++ - 实体的定义中提到了值这个词,那么什么是值呢?

javascript - 如何使用正则表达式从括号中取出单词

regex - 如何 grep 查找 "string\tstring"?

java - 使用正则表达式查找字符串中的子字符串 - JAVA

c++ - 为什么这些条件不适用于模板类型?

c++ - 可以通过重复函数调用来初始化 const std::array 吗?