fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. String input = new Scanner(System.in).useDelimiter("\\Z").next();
  14.  
  15. final Pattern p = Pattern.compile(
  16. "\".*?(?<!\\\\)\"|" + // ignore string literals
  17. "(?s)<%--.*?--%>|" + // ignore JSP comments
  18. "<!--(.*?)-->"); // capture HTML comments in group #1
  19.  
  20. Matcher m = p.matcher(input);
  21. while (m.find()) {
  22. if (m.group(1) != null) {
  23. m.appendReplacement(sb, "<%--$1--%>");
  24. }
  25. }
  26. m.appendTail(sb);
  27. String output = sb.toString();
  28.  
  29. System.out.println(output);
  30. }
  31. }
Success #stdin #stdout 0.14s 321344KB
stdin
<!-- normal HTML comment -->
<%-- normal JSP comment --%>
<%-- <!-- nested HTML comment in JSP comment --> --%>
String variable = "\"<!-- comment inside string literal -->"
stdout
<%-- normal HTML comment --%>
<%-- normal JSP comment --%>
<%-- <!-- nested HTML comment in JSP comment --> --%>
String variable = "\"<!-- comment inside string literal -->"