74

This has been asked several times for several languages but I can't get it to work. I have a string like this

String str = "This is a string.\nThis is a long string.";

And I'm trying to replace the \n with <br /> using

str = str.replaceAll("(\r\n|\n)", "<br />");

but the \n is not getting replaced. I tried to use this RegEx Tool to verify and I see the same result. The input string does not have a match for "(\r\n|\n)". What am i doing wrong ?

Bala R
  • 107,317
  • 23
  • 199
  • 210

7 Answers7

123

It works for me.

public class Program
{
    public static void main(String[] args) {
        String str = "This is a string.\nThis is a long string.";
        str = str.replaceAll("(\r\n|\n)", "<br />");
        System.out.println(str);
    }
}

Result:

This is a string.<br />This is a long string.

Your problem is somewhere else.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
21

For me, this worked:

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

Tip: use regex tester for quick testing without compiling in your environment

Nino van Hooff
  • 3,677
  • 1
  • 36
  • 52
19

A little more robust version of what you're attempting:

str = str.replaceAll("(\r\n|\n\r|\r|\n)", "<br />");
Dolph
  • 49,714
  • 13
  • 63
  • 88
  • 4
    There's no need to do the more lengthy method. `String`'s `replaceAll(...)` does exactly that behind the scenes. This is its method body: `return Pattern.compile(expr).matcher(this).replaceAll(substitute)`: http://www.docjar.com/html/api/java/lang/String.java.html – Bart Kiers Jun 16 '10 at 20:31
11

Since my account is new I can't up-vote Nino van Hooff's answer. If your strings are coming from a Windows based source such as an aspx based server, this solution does work:

rawText.replaceAll("(\\\\r\\\\n|\\\\n)", "<br />");

Seems to be a weird character set issue as the double back-slashes are being interpreted as single slash escape characters. Hence the need for the quadruple slashes above.

Again, under most circumstances "(\\r\\n|\\n)" should work, but if your strings are coming from a Windows based source try the above.

Just an FYI tried everything to correct the issue I was having replacing those line endings. Thought at first was failed conversion from Windows-1252 to UTF-8. But that didn't working either. This solution is what finally did the trick. :)

user3798668
  • 111
  • 1
  • 4
2

It works for me. The Java code works exactly as you wrote it. In the tester, the input string should be:

This is a string.
This is a long string.

...with a real linefeed. You can't use:

This is a string.\nThis is a long string.

...because it treats \n as the literal sequence backslash 'n'.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
0

That should work, but don't kill yourself trying to figure it out. Just use 2 passes.

str  = str.replaceAll("(\r\n)", "<br />");
str  = str.replaceAll("(\n)", "<br />");

Disclaimer: this is not very efficient.

Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • If you're going to do two replacements, then regex is not needed and one could simply do: `str = str.replace("\r\n", "
    ").replace("\n", "
    ");`
    – Bart Kiers Jun 16 '10 at 20:22
  • @BartK I don't think there is a `replace(String,String)` in String class. I was only able to find `replace(Char, Char)` – Bala R Jun 16 '10 at 20:38
  • 5
    @Bala correct, there is no such method. But there is a `replace(CharSequence, CharSequence)`. Note that `String` **is** a `CharSequence`. – Bart Kiers Jun 16 '10 at 20:39
-2

This should work. You need to put in two slashes

str = str.replaceAll("(\\r\\n|\\n)", "<br />");

In this Reference, there is an example which shows

private final String REGEX = "\\d"; // a single digit

I have used two slashes in many of my projects and it seems to work fine!

Kasturi
  • 3,335
  • 3
  • 28
  • 42
  • 2
    Actually you don't need two slashes. – Mark Byers Jun 16 '10 at 20:18
  • not sure what your reference has to do with the rest of your answer. The part *"You need to put in two slashes"* is even plain wrong, to be honest. – Bart Kiers Jun 16 '10 at 20:26
  • like I said: both work (neither did Mark say your suggestion did not work, you just don't need the two backslashes: one will do). The point is: your comment that is **needs** two back slashes (opposed to just one) is still wrong. And `\\d` inside a string literal becomes `\d` while `\n` inside a string literal is still just `\n` (a line break), in other words: you can't compare the two. – Bart Kiers Jun 16 '10 at 20:35
  • 2
    I see you removed all of your comments, which leads me to believe you understand my remarks. I suggest you remove your answer as well since the majority of your answer in not correct (don't worry, you won't loose your points by doing so). If you leave it out here (with the incorrect claims), it is likely it will draw more down-votes. – Bart Kiers Jun 16 '10 at 20:45