With reference to below question - String.replaceAll single backslashes with double backslashes
I wrote a test program, and I found that the result is true in both
With org.apache.commons.lang3.StringEscapeUtils.unescapeJava(...), you can escape most of the common spl.chars and also the unicode characters (converts unicode charset to readable regular character)
The first form \\t
will be expanded to a tab char by the pattern class.
The second form \t
will be expanded to a tab char by Java before it builds a pattern.
In the end, you get a tab char either way.
There are two interpretations of escape sequences going on: first by the Java compiler, and then by the regexp engine. When Java compiler sees two slashes, it replaces them with a single slash. When there is t
following a slash, Java replaces it with a tab; when there is a t
following a double-slash, Java leaves it alone. However, because two slashes have been replaced by a single slash, regexp engine sees \t
, and interprets it as a tab.
I think that it is cleaner to let the regexp interpret \t
as a tab (i.e. write "\\t"
in Java) because it lets you see the expression in its intended form during debugging, logging, etc. If you convert Pattern
with \t
to string, you will see a tab character in the middle of your regular expression, and may confuse it for other whitespace. Patterns with \\t
do not have this problem: they will show you a \t
with a single slash, telling you exactly the kind of whitespace that they match.
Yes, there is a general guideline about escaping: Escape sequences in your Java source get replaced by the Java compiler (or some preprocessor eventually). The compiler will complain about any escape sequences it does not know, e.g. \s
. When you write a String literal for a RegEx pattern, the compiler will process this literal as usual and replace all escape sequences with the according character. Then, when the program is executed, the Pattern class compiles the input String, that is, it will evaluate escape sequences another time. The Pattern class knows \s
as a character class and will therefore be able to compile a pattern containing this class. However, you need to escape \s
from the Java compiler which does not know this escape sequence. To do so, you escape the backslash resulting in \\s
.
In short, you always need to escape character classes for RegEx patterns twice. If you want to match a backslash, the correct pattern is \\\\
because the Java compiler will make it \\
which the Pattern compiler will recognize as the escaped backslash character.