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

",\n,/*characters//doesn't exist inside string, which is not so trivial (since some counting"can be tricky because some of them may have been"literals escaped with\"). but we also can't assume that all"which have\before them are literals because there string could also be"\\"and in this case last"is not"literal but correct end of string representing\character. What you need is parser.