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
You could use something like:
Will match if "foo" is found but "bar" is not.
Notice: you must configure your search engine to use multiline regex (EX: Notepad++ has an option called ". matches newline") because usually the dot represent any character except line break.
The only solution I could find to work is the following Regex:
It finds every file which includes
INCLUDED_TERM
but lacksMISSING_TERM
, regardless of the line.The key is the
\s\S
, which ensures the whole file is searched and not each line.If you want to find it on a single line, use it like this:
You can also use
\
as an escape character, cause you may need it likeclass\.variable
.(?m)\A(?=.*REGEX_TO_FIND)(?!.*MISSING_REGEX.*).*\z
The regex can get kinda tricky but it breaks down into two pieces.
I have an example HERE which shows how you want to find the word
connectReadOnly
in the text, and fail to finddisconnect
. Since the text containsconnectReadOnly
it starts looking for the next piece, not findingdisconnect
. Sincedisconnect
is in the text it fails on the entire string (what you will need for your entire file to match). If you play around with the second piece, the negation part(?!.*disconnect.*)
, you can set that as whatever regex you need. In my example I don't want to finddisconnect
anywhere in my code :) You can easily replace that with your word to search on, or even a more complex regex to "not find".The key is to use multi line mode, which is set using the beginning
(?m)
and then using the start/end of string chars. Using^
and$
to start/end a line, where\A
and\z
start and end a string, thus extending the match over the entire file.EDIT: For the
connectReadOnly
anddisconnect
question use:(?m)\A(?=.*connectReadOnly)(?!.*disconnect.*).*\z
. The updated example can be found here.