2014-09-30 43 views
0

我正在寻找匹配前面没有"]: ""("的字符串。首先,我尝试使用向后看语法的同时的标准只有一个,它的工作原理:负面后视中的替代品

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

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

然后,当我尝试使用或语法两个标准的结合向后看,它打破:

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

我得到一个错误说:

RegexpError: invalid pattern in look-behind 

有没有像Regexp.union需要一个字符串匹配所有表达式什么?任何建议将不胜感激。

+3

任何问题'(?<!\()(?<!\]:)\ b(。+)'? – 2014-09-30 22:17:41

+0

@Rawing - 谢谢!它认为它正在工作,但我需要添加更多的测试来确认。我不知道后视可以链接,但它总是有意义的。 – 2014-09-30 22:22:01

+0

@Rawing - 你应该发表你的评论作为答案。 – 2014-09-30 22:31:30

回答

1

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

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

我喜欢这个答案,因为它对我能够链接它们更具可读性。 – 2014-10-01 22:04:04

0

Oniguruma manual

(?<=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 
+2

我发现这个答案没有错。为什么downvote? – nhahtdh 2014-10-01 05:08:42