如何找到目标词的正则表达式匹配项,但如果该目标词前面有一个否定词(如"not"(,则不会给出匹配项。
否定词需要在目标词之前的一定范围内,这样它就不会回头看太远以获得另一个否定
例:
Target = 'word'
+------------------------------------------+-------------+
| 'bananas is a word' | match |
| '76 is not a word' | NOT a match |
| '76 is not a word but bananas is a word' | match |
+------------------------------------------+-------------+
注意:
'76 is not a word but bananas is a word'
不包含,而是匹配项。这就是为什么我不能使用^$
.
理想情况下,我将能够包含多个否定词,因此正则表达式看起来像这样:
.{1,25}(?<!(isn't|not).{1,10}) word
如果模式始终遵循以下行:
bananas is a word
76 is not a word
76 is not a word but bananas is a word
bananas is a word but 76 is not a word
bananas is a word and bird is a word
76 is not a word and 86 isn't a word either
然后,您可以使用以下正则表达式:
([a-zA-Z0-9]*)sis(?!'nt|snot)sasword
请注意同时使用捕获组和非捕获组,以确保从匹配项中正确排除isn't
和is not
。
正则表达式中的小提琴101