正则表达式:不要将字符串与目标字之前的单词匹配

时间:2018-01-05 03:06:27

标签: python regex

如何找到目标单词的正则表达式匹配,但如果该目标单词前面有一个否定单词(如“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

1 个答案:

答案 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'tis not

Fiddle in Regex101