regex to find files containing one word but not another

Fred picture Fred · Mar 4, 2013 · Viewed 13.7k times · Source

I am trying to quickly find all .java files which contain one term but are missing another term. I'm using MyEclipse 10.7 and its 'Search | File Search' feature, which supports regular expressions.

Will regex work in this scenario? What would the correct regex be?

TIA, Steve

Answer

Fred picture Fred · Mar 13, 2013

The only solution I could find to work is the following Regex:

^(?!.[\s\S]*MISSING_TERM).[\s\S]*INCLUDED_TERM.*$

It finds every file which includes INCLUDED_TERM but lacks MISSING_TERM, regardless of the line.

The key is the \s\S, which ensures the whole file is searched and not each line.