删除 Java 文件中的所有注释

2022-09-01 16:24:11

我有很多评论,如下所示。有没有简单的方法来删除所有评论?

IDE Eclipse Kepler

/* 34:   */

/*

 * JD-Core Version:    0.7.0.1

 */

答案 1

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

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

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

打开带有注释的文件,然后打开“搜索”对话框。()并粘贴上述reg-ex之一,然后选中右侧的复选框。现在,您可以搜索并选择“全部替换”以将其替换为第二个框中键入的任何内容。.javaSearch -> File SearchRegular expression

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


答案 2

为此,我制作了一个开源,您可以删除Java注释。

它支持删除或不删除待办事项。

此外,它还支持JavaScript,HTML,CSS,Properties,JSP和XML注释。

如何使用它的小代码片段:

 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();        
  }

推荐