如何找到目标单词的正则表达式匹配,但如果该目标单词前面有一个否定单词(如“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
答案 0 :(得分:0)
如果模式总是沿着以下几行:
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)\sa\sword
请注意使用捕获和非捕获组,以确保从匹配中正确排除isn't
和is not
。