regexnegative-lookbehind

Multiple negative lookbehind with 2-3 words between and commas


consider that I want to get the phrases that contain party and have been great!.

Therefore, the cases I am missing are those with commas or points that mark a separation between sentences (1) or a maximum of 2 or 3 words between the no|bad and the party (2).

This is my current regex.

(?<!\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)

Solution

  • You may use

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

    See the regex demo.

    Details: