多个负面回顾,中间有 2-3 个词和逗号

Multiple negative lookbehind with 2-3 words between and commas

考虑到我想得到包含party的短语,太棒了!

因此,我遗漏的案例是用逗号或点标记句子之间的分隔 (1) 或在 no|bad 和 party (2) 之间最多有 2 或 3 个单词的案例。

这是我当前的正则表达式。

(?<!\b(no|bad)\b.*)party

is there any party?  --> ok
no party --> ok
it was a bad and poor party --> ok
oh, this was a bad party --> ok
no man, this was great party --> BAD (1) (consider the comma or a point that means end sentence)
no sir it was indeed a barbaric party --> BAD (2) (consider a maximum of two or tree words between no|bad and party)

您可以使用

(?<!\b(?:no|bad)\b(?:[^,\w]+\w+){0,2}[^,\w]+)\bparty\b

参见regex demo

详情:

  • (?<!\b(?:no|bad)\b(?:[^,\w]+\w+){0,2}[^,\w]*) - 如果在当前位置的左侧紧邻有
    • \b(?:no|bad)\b - 一个完整的单词 nobad
    • (?:[^,\w]+\w+){0,2} - 零次、一次或两次出现除逗号或单词字符以外的任何 1+ 个字符,然后是一个或多个单词字符
    • [^,\w]+ - 然后是 , 和单词字符以外的一个或多个字符。
  • \bparty\b - 一个完整的单词 party.