简体   繁体   English

如何在eclipse中用新内容替换搜索到的行?

[英]How to replace a searched line with new contents in eclipse?

I am working on a big project which has thousand of Java Files. 我正在做一个有数千个Java文件的大项目。 What i have to do is replace all the System.out.println("Argument") lines used in the code with log4j logging.I can find the line which uses System.out.println(...) by following regex System\\.out.*; 我要做的是用log4j logging替换代码中使用的所有System.out.println("Argument")行。我可以通过遵循regex System\\.out.*;找到使用System.out.println(...)的行System\\.out.*; . is there a way to replace the println call with 有没有办法将println调用替换为

LGR.info( LGR.isInfoEnabled() ? "Argument": null);

This is how it should look: 它应该是这样的:

Before: 之前:

System.out.println("Argument")

After: 后:

LGR.info( LGR.isInfoEnabled() ? "Argument": null);

You can use File search: 您可以使用文件搜索:

  • Check Case sensitive and Regular expression 检查Case sensitiveRegular expression
  • Containing text: System\\.out\\.println\\((.+)\\); 包含文本: System\\.out\\.println\\((.+)\\);
  • File name patterns: *.java 文件名模式: *.java
  • Click Replace... 点击Replace...
  • Check Regular expression 检查Regular expression
  • With: LGR.info( LGR.isInfoEnabled() ? \\1 : null); 使用: LGR.info( LGR.isInfoEnabled() ? \\1 : null);

If you right-click on a search result in the Search view, there is an option called Replace All ... . 如果在“ Search视图中右键单击搜索结果,则有一个名为“ Replace All ...的选项。 A dialog opens that allows you to enter the replacement text. 将打开一个对话框,允许您输入替换文本。 If a regular expression was used during the search, it also allows to use matcher groups from the regex in the replacement. 如果在搜索过程中使用了正则表达式,它还允许在替换中使用来自正则表达式的匹配器组。

There is also an option Replace selected ... if you don't want to replace all occurences. 如果您不想替换所有出现的事件,则还有一个选项“ Replace selected ...

I think that this should work using search tool (CTRL+F) or File Search (CTRL+H) 我认为这应该使用搜索工具(CTRL+F)或文件搜索(CTRL+H)起作用

search: (System\.out\.println\((.+)\));
replace: LGR.info( LGR.isInfoEnabled() ? $2: null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM