开发者

Regex for matching "non-javadoc" comment in Eclipse

开发者 https://www.devze.com 2023-03-10 01:13 出处:网络
I\'m trying to write a regular expression that matches the (non-javadoc) comments in the format /* * (non-jav开发者_如何学Cadoc)

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

/*
 * (non-jav开发者_如何学Cadoc)
 *
 * 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.


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*


In Perl, it would look like

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号