views:

231

answers:

5

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 ?

A: 

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
If you're going to do two replacements, then regex is not needed and one could simply do: `str = str.replace("\r\n", "<br />").replace("\n", "<br />");`
Bart Kiers
@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
@Bala correct, there is no such method. But there is a `replace(CharSequence, CharSequence)`. Note that `String` **is** a `CharSequence`.
Bart Kiers
A: 

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
Actually you don't need two slashes.
Mark Byers
Both `(\\r\\n|\\n)` and `(\r\n|\n)` will work.
Bart Kiers
@Kasturi, 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
@Kasturi, 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
@Kasturi, 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
+6  A: 

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
+1  A: 

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

str = str.replaceAll("(\r\n|\n\r|\r|\n)", "<br />");
Dolph
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
Nice! Removed the redundant code from my answer then :)
Dolph
+2  A: 

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