Java regex escaped characters

后端 未结 2 1307
小鲜肉
小鲜肉 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 11:52

    Yes there are different. The Java Compiler has different behavior for Unicode Escapes in the Java Book The Java Language Specification section 3.3;

    The Java programming language specifies a standard way of transforming a program written in Unicode into ASCII that changes a program into a form that can be processed by ASCII-based tools. The transformation involves converting any Unicode escapes in the source text of the program to ASCII by adding an extra u - for example, \uxxxx becomes \uuxxxx - while simultaneously converting non- ASCII characters in the source text to Unicode escapes containing a single u each.

    So how this affect the /n vs //n in the Java Doc:

    It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler.

    An a example of the same doc:

    The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\b" matches a word boundary. The string literal "(hello)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\(hello\)" must be used.

提交回复
热议问题