1

Case 1: Taking string input from scanner and replacing \n with -- (Not working)

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replaceAll("\n", "--");
System.out.println(str);

input: "UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n"

Case2: Same thing works if I directly assign string with same value as above.

String str = "UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n";
       
str = str.replaceAll("\n", "--");

PS: I have already tried using \n, line.separater

rs_
  • 105
  • 15
  • Does this answer your question? [Java Regex - Using String's replaceAll method to replace newlines](https://stackoverflow.com/questions/9849015/java-regex-using-strings-replaceall-method-to-replace-newlines) – dnault Aug 13 '20 at 05:01
  • @dnault No, I already tried all this, nothing worked. I think when we take input from console, `\n` is parsed as something else but not able to figure out. – rs_ Aug 13 '20 at 05:20
  • `nextLine()` reads one line, so the line cannot contain a newline character. Are you entering a backslash and an `n` as part of your input? – Ole V.V. Aug 13 '20 at 05:22
  • I am giving this as input : UY9Q3HGjqYE1aHNIG+Rju2hS3WAAEFlakOSGZWffabFpWkeQ\nz4g6mfKoGVR2\nF1QkiHRMZfL4mCvChAuL7gCT3d3SrmxD6lBnOiWiFTPUz4Q=\n – rs_ Aug 13 '20 at 05:24

2 Answers2

1
    String str = "Input\\nwith backslash and n";
    str = str.replaceAll("\\\\n", "--");
    System.out.println(str);

Output:

Input--with backslash and n

We need to escape the backslash twice: To tell the regular expression that a literal backslash is intended we need to put two backslashes. And to tell the Java compiler that we intend literal backslashes in the string, each of those two needs to be entered as two backslashes. So we end up typing four of them.

nextLine() reads one line, so the line cannot contain a newline character. So I have assumes that you were entering a backslash and an n as part of your input.

Less confusing solution

We don’t need to use any regular expression here, and doing that complicates the escaping business. So don’t.

    String str = "Input\\nwith backslash\\nand n\\n";
    str = str.replace("\\n", "--");
    System.out.println(str);

Input--with backslash--and n--

The replace method replaces all occurrences of the literal string given (in spite of not having All in the method name). So now we only need one escape, the one for the Java compiler.

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
  • Try to take this input from console via scanner or bufferedReader, does it still work same way? – rs_ Aug 13 '20 at 05:26
  • It should if you enter a single backslash and an `n` on the console. You try and tell me. – Ole V.V. Aug 13 '20 at 05:27
1

In regular expression if you use single backward slash “\” throws error as it is a escape character. If you use double backward slash “\”, it throws “java.util.regex.PatternSyntaxException: Unexpected internal error near index” exception.

The double backward slash is treated as a single backward slash “\” in regular expression. So four backward slash “\\” should be added to match a single backward slash in a String.

Please try replacing \n with \\n:

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replaceAll("\\\\n", "--");
System.out.println(str);