且构网

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

删除 JavaDoc 注释的工具?

更新时间:2022-10-20 22:56:17

试试这个正则表达式在 eclipse/sed/你最喜欢的支持正则表达式的编辑器中搜索替换.

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

  • ?s 将输入视为单行
  • 一个起始字符串**
  • 零个或多个
    • */的负前瞻
    • 空格或非空格字符
  • 尾随字符串*/

编辑

要解决字符串包含 javadoc 的情况,请使用此正则表达式

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

并用第一个捕获组替换它

/1

那么如果你说这不应该匹配

///** */

或更复杂的情况

我建议,使用像 antlr 这样的解析器工具来使用

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

Debuggex 演示

Is there any tool / Eclipse plugin that can remove all JavaDoc comments in a file?

The normal (non-JavaDoc) comments should be intact after running the tool.

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