...
As another example, consider the Pattern
class used in performing regular expression-related tasks. A string literal used for pattern matching is compiled into an instance of the Pattern
type. When the pattern to be matched contains a sequence of characters identical to one of the Java escape sequences — "\"
and "n"
, for example — the Java compiler treats that portion of the string as a Java escape sequence and transforms the sequence into a an actual newline character. To avoid inserting a insert the newline escape sequence, rather than a literal newline character, the programmer must precede the "\n"
sequence with an additional backslash to prevent the Java compiler from treating it as an escape sequencereplacing it with a newline character. The string constructed from the resulting sequence
...
consequently contains the correct two-character sequence \n
and correctly denotes a the escape sequence for newline character in the pattern.
In general, for a particular escape character of the form \X
, the equivalent Java representation is
...