bob
bob

Reputation: 13

Regex to remove Java comments block containing specific words

I am trying to remove comments containing a specific Java doc comment

Eg. I want to remove this comment block

/**
 * @author: Bob
 * @since 28.mar.2008
 *
 */

But not this block

/** 
 * This class represents ...
 *
 * and so on 
 */

So far I have this regexp:

^/\*\*(.|\s)+?\*/

Which matches a block comment

But I need to some conditionals somewhere in there (eg. block comment that contains "@since" I guess the key is to use look aheads, but my regex fu is not that good at the moment.

Any one who can help me improve this

Thanks in advance Bob

Upvotes: 1

Views: 4945

Answers (2)

Tomalak
Tomalak

Reputation: 338158

Since Java comments cannot be nested (thanks @Paŭlo), there is a regex for that.

You can do it with:

^/\*\*(?=(?:(?!\*/)[\s\S])*?@author: Bob)(?:(?!\*/)[\s\S])*\*/

Explanation follows:

^               # start-of-string
/\*\*           # literal "/**" (start-of-comment)

(?=             # begin positive look-ahead (...followed by)
  (?:           #   begin non-capturing group
    (?!         #     begin negative look-ahead (...not followed by)
      \*/       #       literal "*/"
    )           #     end negative look-ahead
    [\s\S]      #     anything, including newlines
  )*?           #   end group, repeat non-greedily
  @author: Bob  #   literal "@author: Bob"
)               # end positive look-ahead

                # ... now we have made sure there is "@author: Bob"
                #     before the end of the comment

(?:             # begin non-capturing group
  (?!           #   begin negative look-ahead
    \*/         #     literal "*/"
  )             #   end negative look-ahead
  [\s\S]        #   anything, including newlines (this eats the comment)
)*              # end group, repeat

\*/             # literal "*/" (end-of-comment)

Upvotes: 9

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

Couldn't you simply switch off the interpreting of @author and @since by javadoc?

There are the -nosince option to avoid printing the @since block, and @author is by default not included (needs the -author option).

Of course, if you want to obfuscate that you didn't write the source yourself, remove it from the source. (But make sure this is within the license of the code that you got.)

Upvotes: 0

Related Questions