Search code examples
javaregexlatexnewlinereplaceall

java regex detect \n


I am trying to delete LaTeX comments in a string:

input string:

\begin{comment}inside \n comment 1 \end{comment} something outside comments \begin{comment} inside comment 2 \end{comment} after comment 2

output:

\begin{comment}inside comment 1 \end{comment} something outside comments after comment 2

Desirable output:

something outside comments after comment 2

Sample Code:

public static void main(String[] args) {
    String input = "\\begin{comment}inside \n comment 1  \\end{comment}  something outside comments \\begin{comment} inside comment 2\\end{comment} after comment 2";
    System.out.println(input.replaceAll("\\\\begin\\{comment\\}(.*|[\\s]*|\\n*)\\\\end\\{comment\\}", ""));
    }

So the problem is that this regex is not detecting \n.

I used the following link to form the regex :

http://www.regexplanet.com/advanced/java/index.html


Solution

  • Either compile your Pattern with the Pattern.DOTALL option, or add the equivalent flag expression (?s) to your regex, so that . matches \n. Also, you regex doesn't seem to be working, try the following:

    System.out.println(input.replaceAll("(?s)\\\\begin\\{comment\\}.*?\\\\end\\{comment\\}", ""));