I want to remove all type of comments statements from a java source code file. Example:
String str1 = "SUM 10" /*This is a Comments */ ;
String str2 = "SUM 10"; //This is a Comments"
String str3 = "http://google.com"; /*This is a Comments*/
String str4 = "('file:///xghsghsh.html/')"; //Comments
String str5 = "{\"temperature\": {\"type\"}}"; //comments
Expected Output:
String str1 = "SUM 10";
String str2 = "SUM 10";
String str3 = "http://google.com";
String str4 = "('file:///xghsghsh.html/')";
String str5 = "{\"temperature\": {\"type\"}}";
I am using the below regular expression to achieve :
System.out.println(str1.replaceAll("[^:]//.*|/\\\\*((?!=*/)(?s:.))+\\\\*/", ""));
This gives me wrong result for str4 and str5. Please help me to resolve this issue.
Using Andreas solutions:
final String regex = "//.*|/\\*(?s:.*?)\\*/|(\"(?:(?<!\\\\)(?:\\\\\\\\)*\\\\\"|[^\\r\\n\"])*\")";
final String string = " String str1 = \"SUM 10\" /*This is a Comments */ ; \n"
+ " String str2 = \"SUM 10\"; //This is a Comments\" \n"
+ " String str3 = \"http://google.com\"; /*This is a Comments*/\n"
+ " String str4 = \"('file:///xghsghsh.html/')\"; //Comments\n"
+ " String str5 = \"{\"temperature\": {\"type\"}}"; //comments";
final String subst = "$1";
// The substituted value will be contained in the result variable
final String result = string.replaceAll(regex,subst);
System.out.println("Substitution result: " + result);
Its working except str5.