Removing backslashes and newlines (occurring together) in Java

I have a stream of data coming from different channels that I need to clear.

The data is in a specific format, and if a sentence spans several lines, it is separated using the "\" (backslash) that I want to delete. \ Is also present in another part of the text to escape quotes, etc., and I do not want to remove these backslashes. So in the end I want to delete "\\ n".

I tried using regex to remove \ and \ n, but this did not work:

singleLine.replaceAll("(\\\\n|\\\\r)", "");

I am not sure if the regular expression will work in this case.

+1
source share
4 answers

Regex is not required for this; If I were you, I would use ...

 singleLine=singleLine.replace("\\\\n", ""); 

Many people think that the replace method replaces only one, but in fact the only difference is that replaceAll uses regex, and instead of replacing it simply replaces the exact string matches.

If you really want to use the regex, I think you need to do \\\\\\\\ (you have to "collapse" the escape character in Java and in regex, so x4, not just x2)

Explaining this a little more

The only problem in your example: you never set singeLine to anything; I'm not sure if you hid it or missed it.

Edit: Explaining the reasoning for \\\\\\\\ a few more, Java requires you to "\\" represent one \. Regex is also used for the \ character and requires you to do the same for it. If you're just “\\” in Java, the regex parser gets “\”, it skips the character for certain things. You have to give the regexp parser two of them to avoid it, so in Java you need to do "\\\\" to represent a match for one "\"

+5
source

For each pattern in this regular expression, you will need 5 backslash characters.

Using:

 singleLine.replaceAll("(\\\\\n|\\\\\r)", ""); 

The backslash character is both an escape sequence in your string and an escape sequence in regexp. Thus, to represent the literal \ in a regular expression, you need to use 4 characters - your regular expression needs \\ to get an escape slash and each of them needs to be escaped into a java String , and then the other to represent either \n , or \r .

 String str = "string with \\\n newline and \\\n newline ..."; String repl = str.replaceAll("(\\\\\n|\\\\\r)", ""); System.out.println("str: " + str); System.out.println("repl: " + repl); 

Output:

 STR: string with \ newline and \ newline ... REPL: string with newline and newline ... 
+2
source

You need to assign the return value to another String object or to the same object due to String immutability.

 singleLine = singleLine.replaceAll("(\\\\n|\\\\r)", ""); 

More info here

+1
source

Remember that strings are immutable. This means that replaceAll () does not change the string in singleLine. You must use the return value to get the modified string. For example, you can do

 singleLine = singleLine.replaceAll("(\\\\n|\\\\r)", ""); 
+1
source

All Articles