繁体   English   中英

负面回顾的替代方案

[英]Alternatives in negative look-behind

我正在寻找一个不以"]: ""("开头的字符串。首先,我尝试一次仅对其中一个条件使用后向语法,并且可以正常工作:

/(?<!\]: )\b(.+)/i

/(?<!\()\b(.+)/i

然后,当我尝试在后面使用或语法组合这两个条件时,它会中断:

/(?<!(\]: |\())\b(.+)/i

我收到一条错误消息:

RegexpError: invalid pattern in look-behind

是否有Regexp.union类的Regexp.union需要一个字符串来匹配所有表达式? 任何建议将不胜感激。

您可以像这样使用两个连续的lookbehinds:

(?<!\()(?<!\]: )\b(.+)

Oniguruma手册

(?<=subexp)        look-behind
(?<!subexp)        negative look-behind

                     Subexp of look-behind must be fixed character length.
                     [D]ifferent character length is allowed in top level
                     alternatives only.
                     ex. (?<=a|bc) is OK. (?<=aaa(?:b|cd)) is not allowed.

                     In negative-look-behind, captured group isn't allowed, 
                     but shy group(?:) is allowed.

因此,只需将捕获的组替换为害羞的组即可:

/(?<!(?:\]: |\())\b(.+)/i

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM