I need to write a Java method
public static String removeComments(String code);
that removes all Java comments from a String. I know that questions like these have been asked before but:
Using complicated regex-Expressions for multi-line comments like
text = text.replaceAll("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)", " ");
works fine in small tests, but often throws Stackoverflows in my real-world cases (the | in regex leads to branching).Many people posted some "simple" functions they came up with where you can easily construct counter examples (like having // in a String literal, e.g. in a URL).
So is there a reliable, non-recursive way of solving this problem?