eclipse 正则表达式查找包含一个单词但不包含另一个单词的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15209711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
regex to find files containing one word but not another
提问by Fred
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.
我试图快速找到所有包含一个术语但缺少另一个术语的 .java 文件。我正在使用 MyEclipse 10.7 及其“搜索 | 文件搜索功能,支持正则表达式。
Will regex work in this scenario? What would the correct regex be?
正则表达式会在这种情况下工作吗?正确的正则表达式是什么?
TIA, Steve
蒂亚,史蒂夫
回答by Fred
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.
它查找包含INCLUDED_TERM
但缺少 的每个文件MISSING_TERM
,无论行如何。
The key is the \s\S
, which ensures the whole file is searched and not each line.
关键是\s\S
,它确保搜索整个文件而不是每一行。
回答by leventunver
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
.
您也可以\
用作转义字符,因为您可能需要它,例如class\.variable
.
回答by Walls
(?m)\A(?=.*REGEX_TO_FIND)(?!.*MISSING_REGEX.*).*\z
(?m)\A(?=.*REGEX_TO_FIND)(?!.*MISSING_REGEX.*).*\z
The regex can get kinda tricky but it breaks down into two pieces.
正则表达式可能有点棘手,但它分解为两部分。
- Find the matching term/phrase/word. This part isn't too tricky as this is what regex normally looks for.
- Finding the term not present. This is the tricky part, but it's possible.
- 找到匹配的术语/短语/单词。这部分不太棘手,因为这是正则表达式通常要查找的内容。
- 查找不存在的术语。这是棘手的部分,但这是可能的。
I have an example HEREwhich 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".
我这里有一个例子,它显示了你想如何connectReadOnly
在文本中找到这个词,但找不到disconnect
. 由于文本包含connectReadOnly
它开始寻找下一块,而不是寻找disconnect
. 由于disconnect
在文本中,它在整个字符串上失败(您需要匹配整个文件)。如果您玩弄第二个部分,即否定部分(?!.*disconnect.*)
,您可以将其设置为您需要的任何正则表达式。在我的示例中,我不想disconnect
在我的代码中找到任何地方 :) 您可以轻松地将其替换为要搜索的单词,甚至可以使用更复杂的正则表达式来“找不到”。
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.
关键是使用多行模式,使用开头(?m)
然后使用字符串字符的开始/结尾设置。使用^
and$
开始/结束一行, where \A
and\z
开始和结束一个字符串,从而将匹配扩展到整个文件。
EDIT: For the connectReadOnly
and disconnect
question use: (?m)\A(?=.*connectReadOnly)(?!.*disconnect.*).*\z
. The updated example can be found here.
编辑:对于connectReadOnly
和disconnect
问题使用:(?m)\A(?=.*connectReadOnly)(?!.*disconnect.*).*\z
。可以在此处找到更新的示例。
回答by Jamby
You could use something like:
你可以使用类似的东西:
(?<!.*bar)foo(?!.*bar)
Will match if "foo" is found but "bar" is not.
如果找到“foo”但未找到“bar”,则匹配。
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 exceptline break.
注意:您必须将搜索引擎配置为使用多行正则表达式(例如:Notepad++ 有一个名为“.matches newline”的选项),因为通常点表示除换行符之外的任何字符。