Venkata Santosh L
Venkata Santosh L

Reputation: 33

How to remove commented lines in java

I want to remove commented lines in java file. For Example: Java file contains below comments

  1. //this is java
  2. /* I want to remove commented lines */
  3. // I am trying
  4. /* Revision : 25 Added this code for some feature */

I used below regular expression to remove comments. This is removing all types of comments from .java file.

string.replaceAll("(\\/\\/.*)|(\\/\\*([\\S\\s]+?)\\*\\/)", "");

But i have some specific scenario's where i don't want to delete all comments. I'm able to delete all the commented lines(1,2,3 pointed mentioned above) using above pattern.

But I don't want to remove the comment mentioned in 4th point as it contains required information.

Can anyone let me know what is required to change in pattern for not removing such comments?

Note : I know the information, that I should not remove comments contains "Revision : 25"

Upvotes: 2

Views: 1016

Answers (2)

Stefan Ferstl
Stefan Ferstl

Reputation: 5265

You can add a negative lookahead for the "Revision" info in your regex:

(\/\/.*)|(\/\*((?! Revision : \d+)[\S\s]+?)\*\/)

Upvotes: 0

Andremoniy
Andremoniy

Reputation: 34900

As correctly @markspace mentioned in his comment, you will not be able in general to remove multi-line comments /* ... */ using only regular expressions.

This is because regular expressions describes Regular grammar, while multiline /* ... */ comments can be parsed only by Context-free grammar-based parser (because /* is always paired with */, such kind of formula is not describable via Regular grammars - it needs stack and therefore corresponds to Pushdown automata). This issue is similar to parentheses and square brackets issue.

Furthermore, it could be that /* ... */ appears inside string constants, so you also have to distinguish these cases, which can be much more complicated.

This is why in order to completely solve this task you will need to write your own grammar and parser. For these purposes I would suggest you learn about ANTLR which is designed for these things.

Upvotes: 1

Related Questions