【问题标题】:Remove All Comments in Java Files删除 Java 文件中的所有注释
【发布时间】:2014-08-10 23:34:21
【问题描述】:

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

IDE Eclipse 开普勒

/* 34:   */

/*

 * JD-Core Version:    0.7.0.1

 */

【问题讨论】:

标签: java eclipse


【解决方案1】:

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

对我来说,我使用了 Notepad++,它能够一次性替换我项目中的所有文件。

  1. 打开记事本++,按Ctrl + Shift + F

  2. 点击Find in files标签。

  3. 将项目目录根路径粘贴到Directory中

  4. 删除所有 block-cmets(感谢用户 Ahmet Karakaya)

    • 找到什么:\/\*([\S\s]+?)\*\/
    • 替换为:<leave it empty, no empty space>
    • 过滤器:*.java(可以是*.*)如果要在所有文件中搜索
    • 单击查找全部以确保搜索结果是您要替换的内容。
    • 单击在文件中替换
  5. 删除所有单行cmets

    • 查找内容:\/\/([\S\s]+?)\n
    • 替换为:<leave it empty, no empty space>
    • 过滤器:*.java(可以是*.*)如果要在所有文件中搜索
    • 单击查找全部以确保搜索结果是您要替换的内容。
    • 单击在文件中替换

注意:第 5 步将删除所有以 // 开头的行,直到下一个换行符。如果您的代码中有 URL,它也会将其删除。如果您不想编写另一个 REGEX,您始终可以先将 https:// 替换为 https:ZZ 之类的内容,然后在删除所有单行 cmets 后将其替换回原始状态。

【讨论】:

    【解决方案2】:

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

    这只替换//comments

    //(.?+)+
    

    【讨论】:

      【解决方案3】:

      我找到了解决方案Regular Expression with multiple lines search

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

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

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

      使用替换选项我已经从 java 文件中清除了所有 cmets。

      【讨论】:

      • 工作正常!只需勾选正则表达式框
      • 感谢一百万,我有数百个文件,每个文件的所有内容都有 4 行长的空 javadoc cmets。
      • 在 notepad++ 中打开 java 文件,然后使用上面给出的正则表达式 \/*([\S\s]+?)*\/。它对我有用。非常感谢。
      【解决方案4】:

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

      (?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) «$»
      

      【讨论】:

      • 唯一的缺点是,如果你有一个像.."http://www.domain.com"..这样的字符串的行,这也会抓住它
      • 是的......它并不完美......我没有测试很多用例......我逐个使用它。
      • ([^:]//).*?(?&lt;=$) 之类的东西可以避免这些情况
      【解决方案5】:

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

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

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

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

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

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

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

      【讨论】:

      • 那么我如何替换 /**/ 之间和之后的所有 cmets //
      • 对于单行 cmets,只需将光标移至该行(或者如果有多行 cmets 以 // 开头,则选择所有行)并按 Ctrl + \(反斜杠),如果这样做不起作用尝试Ctrl + /(正斜杠)。这取决于你的版本。多行 cmets 也是如此。选择所有行并按 Ctrl + Shift + \(反斜杠)(或 /(正斜杠))。
      • 我认为你遗漏了一些重要的东西。我有 10K 个文件,你不想手动制作它们吗? :)
      • 那么你将不得不编写一个完成这项工作的小程序。
      • 你离问题很远。无论如何,我找到了解决方案。谢谢
      【解决方案6】:

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

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

      【讨论】:

        【解决方案7】:

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

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

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

        【讨论】:

        • 该问题具体说明了如何在 Eclipse 中执行此操作,但如果有人在此问答中寻找 any 删除 cmets 的方法,您的答案将很有用,因此忽略适用于回答上面的问题,+1
        • 我运行它时得到sed: 1: "/\/\*/{:loop;/\/\*.*\*\ ...": unexpected EOF (pending }'s)
        【解决方案8】:

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

        它支持删除或不删除 TODO。

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

        小代码sn-p怎么用:

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

        【讨论】:

        • 有什么可以保存javadoc注释的吗?
        • 你可以使用:.preserveJavaClassHeaders(true).preserveCopyRightHeaders(true)
        【解决方案9】:

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

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

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

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

        【讨论】:

        • \/*.**\/ requular expressşon 找到所有 /* */ 像 cmets。但它没有在 /**/ 之间找到带有换行符的注释
        • 我编辑了我的答案。不确定这是否会起作用,因为我前段时间从 eclipse 切换到了 intellJ。
        【解决方案10】:

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

        请参阅thisthisthis

        【讨论】:

        • 如果您要做的只是在其他地方引用 OP,最好在评论中发布您的链接。无论是否存在链接,答案都应该能够独立存在。
        • 404 , 404 , 嗯
        猜你喜欢
        • 2014-06-13
        • 2018-11-07
        • 2012-11-12
        • 1970-01-01
        • 2011-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-21
        相关资源
        最近更新 更多