删除java文件中的所有类型的注释 [英] Removing all types of comments in java file

查看:372
本文介绍了删除java文件中的所有类型的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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. 两个注释 / *注释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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆