且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用IntelliJ删除源文件注释?

更新时间:2022-05-29 23:13:37

您可以使用替换(或在路径中替换if您要在多个文件中删除注释),然后在文本查找字段中使用此正则表达式:

You can use the "Replace" (or "Replace in Path" if you want to remove comments in multiple files) in the regular expression mode and then use this regular expression in the "Text to find" field:

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/|[ \t]*//.*)

。然后按全部将此替换应用于整个文件或所有选定的文件。这将从您的文件中删除所有块注释和行注释。如果只想删除块注释,请改用正则表达式:

and replace it with an empty string. Then press "All" to apply this replacement to the entire file or all the selected files. This will remove all block comments and line comments from your file. If you want only block comments to be removed, use this regex instead:

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)

如果你想删除行注释,可以使用这个正则表达式:

And if you want to just remove line comments, you can use this regex:

([ \t]*//.*)

,我应该警告,这只工作%99.99次。您可能在您的文件中定义了一个字符串变量,如:

However, I should warn that this works only %99.99 of times. You might have a string variable defined in your file like:

String myStr = "/** I am not a comment */";

这个正则表达式会变成:

This regex will turn this to:

String myStr = "";

:D

希望这有帮助。