Eclipse File Search Dialog - Regular Expression for Group Unions and Negation

后端 未结 1 1904
失恋的感觉
失恋的感觉 2020-12-09 14:21

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 expr

相关标签:
1条回答
  • 2020-12-09 14:44

    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.

    0 讨论(0)
提交回复
热议问题