1

I've this text and I need to replace all \r\n with \n.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\r\n

Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus\r\n

et magnis dis parturient montes, nascetur ridiculus mus.\r\n 

Don12345

When I test on a Java Regex compiler online, the code 1 doesn't work.

Code 1

String result = myText.replaceAll("\r\n", "\n");

But this code works:

Code 2

String result = myText.replaceAll("\\r\\n", "\n"); //second test

And according to this answer, actually should be four \

Why the code on second test works (code 2)?

UPDATE

Actually code 1 works, I'm sorry the mistake.

Code 1 Debug evaluation of code 1

Code 2 Debug evaluation of code 2

enter image description here

So "\r\n" in my text isn't a metacharacter?

Daniela Morais
  • 2,125
  • 7
  • 28
  • 49
  • Because replaceAll() returns a string replacing all the sequence of characters matching regex and the replacement string too. – Amit kumar Jun 19 '20 at 13:54
  • 1
    I just wonder why you do that. It's simpler to do replace("\r", "") then all \r are gone. –  Jun 19 '20 at 14:11
  • 1
    The naming of `String.replace()` and `String.replaceAll()` is really screwed up. Both methods (!) replace all occurences. However, `replace` searches for literal strings and `replaceAll` for regexes. I think the `replaceAll` should have been called `replaceAllRegex` and `replace` should have been called `replaceAll`. – Socowi Jun 19 '20 at 14:30

2 Answers2

0

I do not think the following will work in the intended way with String#replaceAll:

myText.replaceAll("\\r\\n", "\n")

The reason is, String#replaceAll expects a regex and in order to escape \, you need another \ but since it's a regex pattern, you need another pair of \\ (the way you do it e.g. in \\s).

It should be

myText.replaceAll("(\\\\r\\\\n)", "\n");

which means \r\n will be replaced with \n.

Alternatively, you can use String#replace which expects the string to be replaced instead of regex as is the case with String#replaceAll i.e. myText.replace("\\r\\n", "\n") will work in the same way as myText.replaceAll("(\\\\r\\\\n)", "\n") does.

public class Main {
    public static void main(String[] args) {
        String myText = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\\r\\n\n" + "\n"
                + "Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus\\r\\n\n" + "\n"
                + "et magnis dis parturient montes, nascetur ridiculus mus.\\r\\n \n" + "\n" + "Don12345";

        System.out.println(myText);

        System.out.println("---------------------------------------------------------------------------");

        String result = myText.replaceAll("(\\\\r\\\\n)", "\n");
        System.out.println(result);
    }
}

Output:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\r\n

Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus\r\n

et magnis dis parturient montes, nascetur ridiculus mus.\r\n 

Don12345
---------------------------------------------------------------------------
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.


Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus


et magnis dis parturient montes, nascetur ridiculus mus.


Don12345
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • I've debug it and works (I double checked the breakpoint). Also, I needed it bc of this issue (https://github.com/primefaces/primefaces/issues/768).. the text has 200 chars (column is a VARCHAR 200) and a "dataException: data is too long for the column" was thrown. After the replaceAll, the UPDATE works fine on MySQL. – Daniela Morais Jun 19 '20 at 14:02
  • 1
    @DanielaMorais - It means you are not facing any issue. You are just trying to understand something. I shouldn't have answered because your question was not clear. – Arvind Kumar Avinash Jun 19 '20 at 14:11
  • @DanielaMorais - I've updated the answer based on your update. – Arvind Kumar Avinash Jun 19 '20 at 14:38
0

\ is an escape character in regex and can be used to escape itself, meaning that you can write \\n to match the text \n and \\r to \r

Please refer this answer

codeogeek
  • 652
  • 1
  • 8
  • 22