删除 Java 文件中的所有注释

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

Remove All Comments in Java Files

javaeclipse

提问by Ahmet Karakaya

I have many comments like following. Is there simple way to remove all comments?

我有很多像下面这样的评论。有没有简单的方法可以删除所有评论?

IDE Eclipse Kepler

IDE Eclipse 开普勒

/* 34:   */

/*

 * JD-Core Version:    0.7.0.1

 */

采纳答案by Ahmet Karakaya

I have found the solution Regular Expression with multiple lines search.

我找到了带有多行搜索的正则表达式解决方案。

Here are the regular expression used to find two types of comments

这是用于查找两种类型注释的正则表达式

\/\*([\S\s]+?)\*\/and (?s)/\*.*?\*/

\/\*([\S\s]+?)\*\/(?s)/\*.*?\*/

Open the .javafile with comments and open the Search dialog.(Search -> File Search) and paste one of the above reg-ex and check the Regular expressiontick box on the right side. Now you can search and select "Replace All" to replace it with nothing typed in the second box.

打开.java带有注释的文件并打开“搜索”对话框。( Search -> File Search) 并粘贴上述正则表达式之一并选中Regular expression右侧的复选框。现在您可以搜索并选择“全部替换”以将其替换为在第二个框中输入的任何内容。

Using replace-with option I have cleaned all comments from java files.

使用替换选项我已经清除了 java 文件中的所有注释。

回答by TheWhiteLlama

I think eclipse supports regex search and replace. I'd try something like:

我认为 eclipse 支持正则表达式搜索和替换。我会尝试类似的东西:

search: (?s)(?>\/\*(?>(?:(?>[^*]+)|\*(?!\/))*)\*\/)
replace all with no-space-character or nothing literally

also related to the topic: Eclipse, regular expression search and replace

还与主题相关: Eclipse,正则表达式搜索和替换

I edited the regex and tested it: http://regex101.com/r/sU4vI2Not sure if it works in your case.

我编辑了正则表达式并对其进行了测试: http://regex101.com/r/sU4vI2 不确定它是否适用于您的情况。

回答by Chandrayya G K

There is a plugin which doest this in a single click. This will Comment / Remove out all System.Out's.

有一个插件可以通过单击完成此操作。这将注释/删除所有 System.Out。

Refer this, thisand this.

参考这个这个这个

回答by Yasin

Eclipse has several shortcuts for commenting/uncommenting code.

Eclipse 有几个用于注释/取消注释代码的快捷方式。

For single line java code comment : Ctrl+ /(Forwards Slash) and

对于单行java代码注释:Ctrl+ /(正斜杠)和

Single line uncomment : Ctrl+ \(Backslash)

单行取消注释:Ctrl+ \(反斜杠)

For multiple line java code comment : Ctrl+ Shift+ /(Forwards Slash) and

对于多行Java代码注释:Ctrl+ Shift+ /(向前的斜线)和

Multiline uncomment : Ctrl+ Shift+ \(Backslash)

多行取消注释:Ctrl+ Shift+ \(反斜杠)

Note: For multiline comments, select all the lines that you wish to comment/uncomment first.

注意:对于多行注释,请先选择所有要注释/取消注释的行。

Also Ctrl+ Shift+ Lwill open a list of all major shortcuts for Eclipse.

此外Ctrl+ Shift+L将打开Eclipse中的所有主要的快捷键列表。

回答by Ertu?rul ?etin

I made a open source libraryfor this purpose, you can remove Java Comments.

为此我做了一个开源,可以去掉Java Comments。

It supports remove or NOT remove TODO's.

Also it supports JavaScript , HTML , CSS , Properties , JSP and XML Comments too.

它支持删除或不删除 TODO。

它还支持 JavaScript 、 HTML 、 CSS 、 Properties 、 JSP 和 XML 注释。

Little code snippet how to use it:

小代码片段如何使用它:

 public static void main(String[] args) throws CommentRemoverException {

 // root dir is: /Users/user/Projects/MyProject
 // example for startInternalPath

 CommentRemover commentRemover = new CommentRemover.CommentRemoverBuilder()
        .removeJava(true) // Remove Java file Comments....
        .removeJavaScript(true) // Remove JavaScript file Comments....
        .removeJSP(true) // etc.. goes like that
        .removeTodos(false) //  Do Not Touch Todos (leave them alone)
        .removeSingleLines(true) // Remove single line type comments
        .removeMultiLines(true) // Remove multiple type comments
        .startInternalPath("src.main.app") // Starts from {rootDir}/src/main/app , leave it empty string when you want to start from root dir
        .setExcludePackages(new String[]{"src.main.java.app.pattern"}) // Refers to {rootDir}/src/main/java/app/pattern and skips this directory
        .build();

 CommentProcessor commentProcessor = new CommentProcessor(commentRemover);
                  commentProcessor.start();        
  }

回答by alijandro

Since nobody mentioned text processing tools grep, sed, awk, etc. in *NIX, I post my solution here.

由于没有人在 *NIX 中提到文本处理工具grepsedawk等,我在这里发布了我的解决方案。

If your os is *NIX, you can use the text processing tool sedto remove the comments.

如果你的操作系统是*NIX,可以使用文本处理工具sed去除注释。

sed '/\/\*/{:loop;/\/\*.*\*\//{d;b out};N;b loop};:out' yourfile.java

回答by Shaun

You might try using uncrustify(http://uncrustify.sourceforge.net/) to reformat your /* block comments */to // double-slash comments

您可以尝试使用uncrustifyhttp://uncrustify.sourceforge.net/)将您的格式重新格式化/* block comments */// double-slash comments

That would make your regex a bit "safer" -- just look for \*s//lines and delete them (easy sedoperation)

这将使您的正则表达式更“安全”-只需查找\*s//行并删除它们(sed操作简单)

回答by Eddie B

This one is working well for me... I added it as an IntelliJ IDEA search template for JavaDoc and/or single line comments..

这个对我来说效果很好……我将它添加为 JavaDoc 和/或单行注释的 IntelliJ IDEA 搜索模板。

(?sm)(^(?:\s*)?((?:/\*(?:\*)?).*?(?<=\*/))|(?://).*?(?<=$))


Definition

定义



    Options: ^ and $ match at line breaks

    Match the remainder of the regex with the options: dot matches newline (s); ^ and $ match at line breaks (m) ?(?sm)?
    Match the regular expression below and capture its match into backreference number 1 ?(^(?:\s*)?((?:/\*(?:\*)?).*?(?<=\*/))|(?://).*?(?<=$))?
       Match either the regular expression below (attempting the next alternative only if this one fails) ?^(?:\s*)?((?:/\*(?:\*)?).*?(?<=\*/))?
          Assert position at the beginning of a line (at beginning of the string or after a line break character) ?^?
          Match the regular expression below ?(?:\s*)??
             Between zero and one times, as many times as possible, giving back as needed (greedy) ???
             Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) ?\s*?
                Between zero and unlimited times, as many times as possible, giving back as needed (greedy) ?*?
          Match the regular expression below and capture its match into backreference number 2 ?((?:/\*(?:\*)?).*?(?<=\*/))?
             Match the regular expression below ?(?:/\*(?:\*)?)?
                Match the character “/” literally ?/?
                Match the character “*” literally ?\*?
                Match the regular expression below ?(?:\*)??
                   Between zero and one times, as many times as possible, giving back as needed (greedy) ???
                   Match the character “*” literally ?\*?
             Match any single character ?.*??
                Between zero and unlimited times, as few times as possible, expanding as needed (lazy) ?*??
             Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) ?(?<=\*/)?
                Match the character “*” literally ?\*?
                Match the character “/” literally ?/?
       Or match regular expression number 2 below (the entire group fails if this one fails to match) ?(?://).*?(?<=$)?
          Match the regular expression below ?(?://)?
             Match the characters “//” literally ?//?
          Match any single character ?.*??
             Between zero and unlimited times, as few times as possible, expanding as needed (lazy) ?*??
          Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) ?(?<=$)?
             Assert position at the end of a line (at the end of the string or before a line break character) ?$?

回答by Wesos de Queso

I've done this with the Ctrl+Fcommand and this regex.

我已经用Ctrl+F命令和这个正则表达式完成了这个。

This only replace //comments

这只能替换 //comments

//(.?+)+

enter image description here

在此处输入图片说明