Tool to remove JavaDoc comments?

泄露秘密 提交于 2019-11-28 06:04:05

Try this regex to search replace either in eclipse / sed / your favorite editor that has regex support.

/\*\*(?s:(?!\*/).)*\*/ 
  • ?s treat input as single line
  • a starting string \**
  • zero or more
    • negative lookahead for */
    • space or non space char
  • a trailing string */

Edit

To tackle cases where strings containing javadoc, use this regex

((?<!\\)"([^"]|(?<=\\)")*")|(/\*\*(?s:(?!\*/).)*\*/) 

and replace it with first capture group

/1 

Then if you say this shouldn't match

///** */ 

or more complex cases

I suggest, using a parser tool like antlr to parse using java grammar for tokens like comments and strings and remove elements that you don't want


Sometime I find my own regex answers cryptic !!

So here is some visual feedback

((?!\\)"([^"]|(?=\\)")*")|(/\*\*(?:(?!\*/).)*\*/) 

Debuggex Demo


/\*\*(?:(?!\*/).)*\*/ 

Debuggex Demo

The answer of Prashant Bhate didn't work for me in eclipse ide:

/\*\*(?s:(?!\*/).)*\*/ 

I had to use

/\*/*(?s:(?!\*/).)*\*/ 

instead.

I made a open source library for this purpose , its called CommentRemover you can remove single line and multiple line Java Comments.

It supports remove or NOT remove TODO's.
Also it supports JavaScript , HTML , CSS , Properties , JSP and XML Comments too.

Little code snippet how to use it(There is 2 type usage):

First way InternalPath

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

Second way ExternalPath

 public static void main(String[] args) throws CommentRemoverException {   // example for externalInternalPath   CommentRemover commentRemover = new CommentRemover.CommentRemoverBuilder()         .removeJava(true) // Remove Java file Comments....         .removeJavaScript(true) // Remove JavaScript file Comments....         .removeJSP(true) // etc..         .removeTodos(true) // Remove todos         .removeSingleLines(false) // Do not remove single line type comments         .removeMultiLines(true) // Remove multiple type comments         .startExternalPath("/Users/user/Projects/MyOtherProject")// Give it full path for external directories         .setExcludePackages(new String[]{"src.main.java.model"}) // Refers to /Users/user/Projects/MyOtherProject/src/main/java/model and skips this directory.         .build();   CommentProcessor commentProcessor = new CommentProcessor(commentRemover);                   commentProcessor.start();           } 

The following works for me in vim:

/\/\*\*\_.\{-}\*\/ 

The \_.\{-} pattern matches any character (.), including the newline (\_), as few as possible (\{-}).

This matches against multi-line Javadoc comments such as:

/**  * My Javadoc comment here  */ 

But will not match against comments such as:

// My non-Javadoc comment 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!