用于匹配“非 javadoc”的正则表达式在 Eclipse 中评论

发布于 2024-11-10 11:27:41 字数 441 浏览 2 评论 0原文

我正在尝试编写一个与 (non-javadoc) 注释相匹配的正则表达式

/*
 * (non-javadoc)
 *
 * some other comment here
 *
 */

到目前为止,我有 (?s)/\*\R.*?non-Javadoc .*?\*/,但这实际上匹配太多了。我的文件顶部有一个标题,类似于

/*
 * header text
 */
 public class MyClass {

 }

文件顶部的 /* ,但我真的只想匹配生成的 (非javadoc) 注释。谁能帮我修复这个正则表达式?

编辑:我正在尝试使用 Eclipse 查找/替换对话框,但如果需要,我愿意使用外部工具。

I'm trying to write a regular expression that matches the (non-javadoc) comments in the format

/*
 * (non-javadoc)
 *
 * some other comment here
 *
 */

So far I have (?s)/\*\R.*?non-Javadoc.*?\*/, but that is actually matching too much. I have a header at the top of my file that is something like

/*
 * header text
 */
 public class MyClass {

 }

and it is matching the /* at the top of the file, but I really only want to match the generated (non-javadoc) comment. Can anyone help me fix up this regex?

EDIT: I'm trying to use the Eclipse Find/Replace dialog, but I am open to using external tools if needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老娘不死你永远是小三 2024-11-17 11:27:42

这应该可以做到:

(?s)/\*[^*](?:(?!\*/).)*\(non-javadoc\)(?:(?!\*/).)*\*/

/\*[^*] 匹配 C 风格注释 (/* */) 的开头,但不匹配 JavaDoc 注释 (/** */)

(?!\*/). 匹配任何单个字符,除非它是 */ 序列的开头。搜索 (?:(?!\*/).)* 而不是 .*? 会使匹配不可能在一条评论中开始并在另一条评论中结束。

更新:(迟来的)对 Jacek 评论的回应:是的,您可能需要在正则表达式的末尾添加一些内容,这样您就可以用空字符串替换它,而不会留下太多内容代码中的空白行。但雅采克的解决方案比实际需要的更复杂。您需要添加的只是 \s*

\R 转义序列匹配多种换行符,包括 Unicode 行分隔符 (\u2028)和段落分隔符 (\u2029) 和 DOS/网络​​回车+换行序列 (\r\n)。但这些都是空白字符,因此 \s 匹配它们(至少在 Eclipse 中;根据 文档,相当于[\t\n\f\r\p{Z}])。

Jacek 添加的 \s* 仅用于匹配换行符之前可能存在的任何水平空白(空格或制表符)以及换行符后面的缩进。 (您必须删除它,因为您没有删除注释第一行之前的缩进。)但事实证明 \s* 可以完成整个工作:

(?s)/\*[^*](?:(?!\*/).)*\(non-javadoc\)(?:(?!\*/).)*\*/\s*

This should do it:

(?s)/\*[^*](?:(?!\*/).)*\(non-javadoc\)(?:(?!\*/).)*\*/

/\*[^*] matches the beginning of a C-style comment (/* */) but not a JavaDoc comment (/** */)

(?!\*/). matches any single character unless it's the beginning of a */ sequence. Searching for (?:(?!\*/).)* instead of .*? makes it impossible for a match to start in one comment and end in another.

UPDATE: In (belated) response to the comment by Jacek: yes, you'll probably want to add something to the end of the regex so you can replace it with an empty string and not leave a lot of blank lines in your code. But Jacek's solution is more complicated than it needs to be. All you need to add is \s*

The \R escape sequence matches many kinds of newline, including the Unicode Line Separator (\u2028) and Paragraph Separator (\u2029) and the DOS/network carriage-return+linefeed sequence (\r\n). But those are all whitespace characters, so \s matches them (in Eclipse, at least; according to the docs, it's equivalent to [\t\n\f\r\p{Z}]).

The \s* in Jacek's addition was only meant to match whatever horizontal whitespace (spaces or tabs) might exist before the newline, plus the indentation following it. (You have to remove it because you're not removing the indentation before the first line of the comment.) But it turns out \s* can do the whole job:

(?s)/\*[^*](?:(?!\*/).)*\(non-javadoc\)(?:(?!\*/).)*\*/\s*
李不 2024-11-17 11:27:42

在 Perl 中,它看起来像

/
   \/\*
   (?: (?! \*\/ ) . )*
   non-javadoc
   (?: (?! \*\/ ) . )*
   \*\/
/sx

In Perl, it would look like

/
   \/\*
   (?: (?! \*\/ ) . )*
   non-javadoc
   (?: (?! \*\/ ) . )*
   \*\/
/sx
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文