有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

eclipse删除Java文件中的所有注释

我有很多类似下面的评论。有没有简单的方法删除所有评论

IDE日食开普勒

/* 34:   */

/*

 * JD-Core Version:    0.7.0.1

 */

共 (6) 个答案

  1. # 1 楼答案

    我用Ctrl+F命令和这个正则表达式完成了这项工作

    这只会替换//comments

    //(.?+)+
    

    enter image description here

  2. # 2 楼答案

    因为没有人在*NIX中提到文本处理工具grepsedawk等等,所以我在这里发布我的解决方案

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

    sed '/\/\*/{:loop;/\/\*.*\*\//{d;b out};N;b loop};:out' yourfile.java
    
  3. # 3 楼答案

    这个对我来说很好。。。我将其添加为一个IntelliJ IDEA搜索模板,用于JavaDoc和/或单行评论

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

    定义


        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) «$»
    
  4. # 4 楼答案

    我做了一个开源的library为了这个目的,你可以删除Java注释

    它支持删除或不删除TODO。

    它还支持JavaScript、HTML、CSS、属性、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();        
      }
    
  5. # 5 楼答案

    我认为eclipse支持正则表达式搜索和替换。 我想试试这样:

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

    也与主题相关: Eclipse, regular expression search and replace

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

  6. # 6 楼答案

    我找到了解决办法

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

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

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

    使用replace-with选项,我已经清除了java文件中的所有注释