Eclipse File Search Dialog - Regular Expression for Group Unions and Negation
eclipse, java, regex
Solution
When you want to make the search efficient you have to be aware of the different goals: The Eclipse Search function looks for all occurrences but you want to check for the presence of a word only.
For a single word you can just search for `word` but since you want to search for combinations using unbounded quantifiers this does not perform very well.
So the first thing you have to do is to stop Eclipse (the regex engine) from checking for a match at every character position of a file by adding the anchor `\A` which stands for “beginning of the file”. Then skip as little characters as possible and search for a literal word match to check for the presence:
`(?s)\A.*?word` will search for the first occurrence of `word` but not for any further.
Expanding it to check for two words in order is easy:
(1) `(?s)\A.*?word1.*?word2` Just checks for one occurrence of each word in order but nothing more.
For checking of the presence or absence without an order you can use a look-ahead:
(2) `(?s)\A(?!.*?word2).*?word1` Simply negate the look-ahead to tell that `word2` must not be present…
(3) `(?s)\A(?=.*?word1).*?word2` If one match for `word1` is present find one match for `word2`; of course, `word1` and `word2` are interchangeable.
(4) `(?s)\A(?!.*?word1).?` and just use the negative look-ahead to search for the absence of `word1` only; if absent `.?` just matches a single optional character as an empty regex won’t match anything in `DOTALL` mode.
(5) `(?s)\A.*?(word1|word2)` Telling that either `word1` or `word2` satisfies is straight-forward.
Of course, if you are looking for whole words, the `word` placeholders above have to be replaced by `\bactualwordcharacters\b`.
Problem
I want to search two(or more) words(Or you can say group) in a given file type inside any given project/workspace. I need an Efficient regular expression for the following queries(In Multi line mode) to use it eclipse File Search dialog(See below image. You can open search dialog by pressing Ctrl+H) - Word 1 comes first then Word 2 comes - Word 1 present But Word 2 MUST NOT present ANYWHERE. - Both Word 1 and Word 2 MUST present ANYWHERE in the file(ORDER does't matter i.e Group union) - Word 1 MUST NOT present ANYWHERE in the file.(i.e group negation) - Word 1 Present OR Word 2 Present ANYWHERE in a file(ORDER doesn't matter) Edit I got for 1st - (?m)(?s)(Word 1).*(Word 2) But not for others.