regex to find files containing one word but not an

2019-02-04 17:49发布

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

4条回答
Rolldiameter
2楼-- · 2019-02-04 18:28

You could use something like:

(?<!.*bar)foo(?!.*bar)

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-04 18:30

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.

查看更多
在下西门庆
4楼-- · 2019-02-04 18:31

If you want to find it on a single line, use it like this:

^(?!.*MISSING_TERM).*INCLUDED_TERM.*$

You can also use \ as an escape character, cause you may need it like class\.variable.

查看更多
乱世女痞
5楼-- · 2019-02-04 18:31

(?m)\A(?=.*REGEX_TO_FIND)(?!.*MISSING_REGEX.*).*\z

The regex can get kinda tricky but it breaks down into two pieces.

  1. Find the matching term/phrase/word. This part isn't too tricky as this is what regex normally looks for.
  2. Finding the term not present. This is the tricky part, but it's possible.

I have an example HERE which shows how you want to find the word connectReadOnly in the text, and fail to find disconnect. Since the text contains connectReadOnly it starts looking for the next piece, not finding disconnect. Since disconnect 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 find disconnect 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 and disconnect question use: (?m)\A(?=.*connectReadOnly)(?!.*disconnect.*).*\z. The updated example can be found here.

查看更多
登录 后发表回答