我有很多评论如下。有没有简单的方法来删除所有评论?
IDE Eclipse Kepler
/* 34: */
/*
* JD-Core Version: 0.7.0.1
*/
答案 0 :(得分:45)
我找到了解决方案Regular Expression with multiple lines search。
以下是用于查找两种评论类型的正则表达式
\/\*([\S\s]+?)\*\/
和(?s)/\*.*?\*/
打开包含评论的.java
文件并打开“搜索”对话框。(Search -> File Search
)并粘贴上述注册表中的一个,然后选中右侧的Regular expression
复选框。现在,您可以搜索并选择"全部替换"用第二个框中输入的内容替换它。
使用replace-with选项我已经清除了java文件中的所有注释。
答案 1 :(得分:4)
为此,我创建了一个开源library,您可以删除Java Comments。
支持删除或不删除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();
}
答案 2 :(得分:3)
由于* NIX中没有人提及文字处理工具grep
,sed
,awk
等,我在此处发布了我的解决方案。
如果您的操作系统是* NIX,则可以使用文本处理工具sed
删除评论。
sed '/\/\*/{:loop;/\/\*.*\*\//{d;b out};N;b loop};:out' yourfile.java
答案 3 :(得分:3)
答案 4 :(得分:2)
这个对我来说效果很好......我将它添加为用于JavaDoc的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) «$»
答案 5 :(得分:0)
我认为eclipse支持正则表达式搜索和替换。 我尝试过类似的事情:
search: (?s)(?>\/\*(?>(?:(?>[^*]+)|\*(?!\/))*)\*\/)
replace all with no-space-character or nothing literally
也与主题相关: Eclipse, regular expression search and replace
我编辑了正则表达式并对其进行了测试: http://regex101.com/r/sU4vI2 不确定它是否适用于你的情况。
答案 6 :(得分:0)
答案 7 :(得分:0)
Eclipse有几个用于注释/取消注释代码的快捷方式。
对于单行java代码注释: Ctrl + / (Forwards Slash)和
单行取消注释: Ctrl + \ (反斜杠)
对于多行java代码注释: Ctrl + Shift + / (Forwards Slash)和
多行取消注释: Ctrl + Shift + \ (反斜杠)
注意:对于多行注释,请选择您希望首先注释/取消注释的所有行。
Ctrl + Shift + L 将打开Eclipse的所有主要快捷方式列表。
答案 8 :(得分:0)
您可以尝试使用uncrustify
(http://uncrustify.sourceforge.net/)将/* block comments */
重新格式化为// double-slash comments
这会让你的正则表达式更“安全” - 只需查看\*s//
行并删除它们(简单sed
操作)
答案 9 :(得分:0)
我有很多像下面这样的评论。有没有简单的方法可以删除所有评论?
对我来说,我使用 Notepad++,它能够一次性替换我项目中的所有文件。
打开 Notepad++,按 Ctrl + Shift + F
点击 Find in files
标签。
将项目目录根路径粘贴到目录中
删除所有屏蔽评论(感谢用户 Ahmet Karakaya)
\/\*([\S\s]+?)\*\/
<leave it empty, no empty space>
*.java
(可以是 *.*
)如果要搜索所有文件删除所有单行注释
\/\/([\S\s]+?)\n
<leave it empty, no empty space>
*.java
(可以是 *.*
)如果要搜索所有文件注意:第 5 步将删除所有以 //
开头的行,直到下一个换行符。如果您的代码中有 URL,它也会将其删除。如果您不想编写另一个 REGEX,您始终可以先将 https://
替换为类似 https:ZZ
的内容,然后在删除所有单行注释后将其替换回原始状态。