Perl re负面看后面的可变长度错误

Bry*_*anK 5 regex perl

当没有单引号或美元符号前面时,这个正则表达式适用于匹配模式'ab_':

/(?<!('|\$))ab_/
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试,例如,在单引号前添加一个括号

/(?<!(\['|\$))ab_/
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

Variable length lookbehind not implemented in regex;
Run Code Online (Sandbox Code Playgroud)

这个错误意味着什么,有没有办法让第二个例子有效?由于我不是专家,所以很可能忽略了一些基本的东西,所以请指出我所遗漏的任何东西.

rua*_*akh 7

该错误意味着在Perl中,lookbehind断言必须具有固定长度模式.('|\$)很好,因为模式只匹配长度为1的子字符串,但(\['|\$)可以匹配长度为1的子字符串($)或长度为2的子字符串([').

在您的情况下,您可以通过使用两个单独的lookbehinds来解决这个问题,每个lookbehinds都有一个固定长度的模式,每个情况都要排除一个:

/(?<!\[')(?<!\$)ab_/
Run Code Online (Sandbox Code Playgroud)