Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Regex - Remove comments

Tags:

java

regex

I want to remove comments in Java code. I have seen a lot of examples, but each was written wrong.

Here is example of code:

String somestring = "http://google.com"; // "//google.com";" is going to be removed

Another example:

    get.setHeader("Accept", "*/*"); // "/*");" and later is going to be removed too

But I want right regular expression which handles those cases

I tried: http://ostermiller.org/findcomment.html Regular expression to remove comment and other popular examples

It should handle common cases:

somemethod();//it should be removed
somemethod(); /* some comment that may end on other line */

But should be handled and other situations:

String somestring = "http://google.com"; // url shouldn't be touched
get.setHeader("Accept", "*/*"); // "*/*" shouldn't be touched too
like image 300
BuGiZ400 Avatar asked Mar 17 '23 02:03

BuGiZ400


1 Answers

Already commented this but lets see how far we get. Java doesn't do regex literals so stripping that one from this answer we get the following regex:

((['"])(?:(?!\2|\\).|\\.)*\2)|\/\/[^\n]*|\/\*(?:[^*]|\*(?!\/))*\*\/

Regular expression visualization

Debuggex Demo

If we then "replace" every match with the first capture group, every match that doesn't have a capture group to begin with (i.e. a comment) is removed:

Regex101 substitution Demo

A explanation of the more generic "match this except in conditions a|b|c"-technique I employed is available here.

like image 114
asontu Avatar answered Mar 29 '23 09:03

asontu