Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for matching "non-javadoc" comment in 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.

like image 345
Jeff Storey Avatar asked May 31 '11 22:05

Jeff Storey


1 Answers

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*
like image 115
Alan Moore Avatar answered Nov 19 '22 15:11

Alan Moore