Java regex escaped characters

后端 未结 2 1265
小鲜肉
小鲜肉 2021-01-22 11:45

When matching certain characters (such as line feed), you can use the regex \"\\\\n\" or indeed just \"\\n\". For example, the following splits a string into an array of lines:<

2条回答
  •  不思量自难忘°
    2021-01-22 12:08

    There is no difference in the current scenario. The usual string escape sequences are formed with the help of a single backslash and then a valid escape char ("\n", "\r", etc.) and regex escape sequences are formed with the help of a literal backslash (that is, a double backslash in the Java string literal) and a valid regex escape char ("\\n", "\\d", etc.).

    "\n" (an escape sequence) is a literal LF (newline) and "\\n" is a regex escape sequence that matches an LF symbol.

    "\r" (an escape sequence) is a literal CR (carriage return) and "\\r" is a regex escape sequence that matches an CR symbol.

    "\t" (an escape sequence) is a literal tab symbol and "\\t" is a regex escape sequence that matches a tab symbol.

    See the list in the Java regex docs for the supported list of regex escapes.

    However, if you use a Pattern.COMMENTS flag (used to introduce comments and format a pattern nicely, making the regex engine ignore all unescaped whitespace in the pattern), you will need to either use "\\n" or "\\\n" to define a newline (LF) in the Java string literal and "\\r" or "\\\r" to define a carriage return (CR).

    See a Java test:

    String s = "\n";
    System.out.println(s.replaceAll("\n", "LF")); // => LF
    System.out.println(s.replaceAll("\\n", "LF")); // => LF
    System.out.println(s.replaceAll("(?x)\\n", "LF")); // => LF
    System.out.println(s.replaceAll("(?x)\\\n", "LF")); // => LF
    System.out.println(s.replaceAll("(?x)\n", "")); 
    // => 
    //
    

    Why is the last one producing +newline+? Because "(?x)\n" is equal to "", an empty pattern, and it matches an empty space before the newline and after it.

提交回复
热议问题