I'm trying to build a regex somewhat like this:
[match-word] ... [exclude-specific-word] ... [match-word]
This seems to work with a negative look-ahead, but I'm running into a problem when I have a case like this:
[match-word] ... [exclude-specific-word] ... [match-word] ... [excluded word appears again]
I want the above sentence to match, but the negative look-ahead between the first and the second matched word "spills over" so the second word is never matched.
Let's look at a practical example.
I wan't to match every sentence which has the word "i" and the word "pie", but not the word "hate" in between those two words. I have these three sentences:
i sure like eating pie, but i love donuts <- Want to match this
i sure like eating pie, but i hate donuts <- Want to match this
i sure hate eating pie, but i like donuts <- Don't want to match this
I have this regex:
^i(?!.*hate).*pie - have removed the word boundaries for clarity, original is: ^i\b(?!.*\bhate\b).*\bpie\b
Which matches the first sentence, but not the second one, because the negative look-ahead scans the whole string.
Is there a way to limit the negative look-ahead, so that it's satisfied if it encounters "pie" before it encounters "hate"?
Note: in my implementation, there may be other terms following this regex (it's built dynamically from a grammar search engine), for instance:
^i(?!.*hate).*pie.*donuts
I'm currently using JRegex, but could probably switch to JDK Regex if necessary
Update: I forgot to mention something in my initial question:
It's possible that the "negative construct" exists further in the sentence, and I do want to match the sentence if it's possible even if the "negative" construct exists further up.
To clarify, look at these sentences:
i sure like eating pie, but i love donuts <- Want to match this
i sure like eating pie, but i hate donuts <- Want to match this
i sure hate eating pie, but i like donuts <- Don't want to match this
i sure like eating pie, but i like donuts and i hate making pie <- Do want to match this
rob's answer works perfectly for this extra constraint, so I'm accepting that one.