删除java文件中的所有类型的注释 [英] Removing all types of comments in java file
问题描述
我有一个java项目,我在许多位置在项目中的各种java文件中使用了注释。现在我需要删除所有类型的注释:单行,多行注释。
请提供自动化删除评论。使用工具或在eclipse等。
I have a java project and i have used comments in many location in various java files in the project. Now i need to remove all type of comments : single line , multiple line comments . Please provide automation for removing comments. using tools or in eclipse etc.
目前我手动尝试删除所有的commetns
Currently i am manually trying to remove all commetns
推荐答案
您可以通过在项目中搜索以下正则表达式来删除所有单行或多行块注释(但不包括带有 //
的行注释) (
You can remove all single- or multi-line block comments (but not line comments with //
) by searching for the following regular expression in your project(s)/file(s) and replacing by $1
:
^([ ^\r\\\
] *?(?:(?<=')[^\r\\\
] *? \\ n] *?[^\r\\\
] *?)*?)(?/ / ] [^ \ *] *)*?\ * + /
可能必须多次执行。
此正则表达式避免了以下陷阱:
This regular expression avoids the following pitfalls:
-
两个注释
/ *注释1 * / foo(); / *注释2 * /
以星号开头的注释: // ***注释***
字面量: stringbuilder.append(/ *);
;如果注释之前的单引号内有双引号
Comment delimiters inside string literals: stringbuilder.append("/*");
; also if there is a double quote inside single quotes before the comment
要删除所有单行注释,请在项目/文件中搜索以下正则表达式,并替换为 $ 1
:
To remove all single-line comments, search for the following regular expression in your project(s)/file(s) and replacing by $1
:
^([^\r\\\
] *? ; =')[^\r\\\
] *?|(?<!')[^\r\\\
] *? ?)*?)\s * // [^ \r\\\
] *
此正则表达式还避免了注释分隔符内部双引号,但不检查多行注释,因此 / * // * /
将不正确地删除。
This regular expression also avoids comment delimiters inside double quotes, but does NOT check for multi-line comments, so /* // */
will be incorrectly removed.
这篇关于删除java文件中的所有类型的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!