如何在 Eclipse 中搜索和替换 2 行(一起)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3164453/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:29:23  来源:igfitidea点击:

How to search and replace 2 lines (together) in Eclipse?

eclipseeditorreplace

提问by Sam

I would like to search multiple files via eclipse for the following 2 lines:

我想通过 eclipse 在多个文件中搜索以下两行:

@Length(max = L_255)
private String description;

and replace them with these two:

并用这两个替换它们:

@Length(max = L_255, message="{validator.description.len}")
private String description;

采纳答案by VonC

Search are multi-line by default in Eclipse when you are using regex:

当您使用正则表达式时,默认情况下 Eclipse 中的搜索是多行的:

(\@Length\(max = L_255)\)([\r\n\s]+private)

I would like to add "private String description;"

我想添加“私人字符串描述;”

(\@Length\(max = L_255)\)([\r\n\s]+private\s+?String\s+description\s*?;)

replaced by:

取而代之:

, message="{validator.description.len}")

It works perfectly in a File Search triggered by a CTRL-H.

它在由CTRL-触发的文件搜索中完美运行H

Eclipse multi-line search

Eclipse 多行搜索

As mentioned in Tika's answer, you can directly copy the two lines selected in the "Containing Text" field: those lines will be converted as a regexpfor you by Eclipse.

正如Tika回答中所述,您可以直接复制在“包含文本”字段中选择的两行:Eclipse会将这些行转换为正则表达式

回答by Mario Balaban

Another tip on how to get the regex for a selected block. Open one of the files that contains the multiple lines (multiline) to search or replace. Click Ctrl+Fand select "Regular expression". Close the Find/Replace window. Select the block you need and click again Ctrl+Fto open the Find/Replace window. Now in the Find text box you have the regular expression that exactly matches your selection block.

关于如何获取选定块的正则表达式的另一个技巧。打开包含要搜索或替换的多行(多行)的文件之一。单击Ctrl+F并选择“正则表达式”。关闭查找/替换窗口。选择您需要的块并再次单击Ctrl+F打开查找/替换窗口。现在在 Find 文本框中,您有与您的选择块完全匹配的正则表达式。

(I discovered this, only after creating manually a regexp for very long block :)

(我发现了这一点,只有在为很长的块手动创建正则表达式之后:)

回答by Tika

CTRL+H does take two lines if you use regexp (and you don't have to write the regexp by yourself, eclipse does that for you).

如果您使用正则表达式,CTRL+H 确实需要两行(并且您不必自己编写正则表达式,eclipse 会为您完成)。

  1. Select your lines.
  2. Click CTRL+H. The search dialog opens up. If "Regular expression" is already checked, eclipse will have converted the two lines you search for into regexp for you, click Search. If "Regular expression" if not already checked", check it and click Cancel (eclipse remembers your choise).
  3. Select your lines again.
  4. Click CTRL+H. The search dialog opens up. This time "Regular expression" is already selected. eclipse will have converted the two lines you search for into regexp for you, click Search.
  1. 选择您的线路。
  2. 单击 CTRL+H。搜索对话框打开。如果已经勾选了“Regular expression”,eclipse会帮你把搜索到的两行转换成regexp,点击搜索。如果“Regular expression”(如果尚未选中),请选中它并单击“取消”(eclipse 会记住您的选择)。
  3. 再次选择您的线路。
  4. 单击 CTRL+H。搜索对话框打开。这次已经选择了“正则表达式”。eclipse 会将您搜索的两行转换为正则表达式,单击“搜索”。

回答by Steve Chambers

A quick tip for including multiple lines as part of a manually constructed regular expression:

包含多行作为手动构建的正则表达式的一部分的快速提示:

Where you would normally use .*to match any character zero or more times, instead consider using something like (?:.|\r?\n)*. Or put an extra ?at the end to make it non-greedy.

在通常用于.*匹配任何字符零次或多次的地方,请考虑使用类似(?:.|\r?\n)*. 或者?在末尾添加一个额外的内容以使其不贪婪。

Explanation: .doesn't match new lines so need to do an "either-or": The parentheses match either the .before the pipe or the new line after it. The ?after \rmakes the carriage return before the line feed optional to allow Windows or Unix new lines. The ?:excludes the whole thing as a capturing group (which helps to avoid a stack overflow).

解释.不匹配新行,所以需要做一个“非此即彼”:括号匹配.管道之前或之后的新行。在?\r使得前行回车喂可选允许Windows或Unix的新行。在?:不包括整个事情作为捕获组(这有助于避免堆栈溢出)。

回答by Bart van Heukelom

Select the folder that contains all your files and press Ctrl+H.

选择包含所有文件的文件夹,然后按Ctrl+ H