Regex patterns use \
as escape character, but so does Java. So to get a single escape (\
) in a regex pattern you should write: \\
. To escape an escape inside a regex, double the pattern: \\\\
.
Of course that's extremely tedious, made all the worse because regexes have a ton of escape sequences like that. Which is why Java regexes also support “quoting” litteral parts of the pattern and this allows you to write your pattern as: \\Q[]\\E
.
EDIT: As the other answer hints at: java.util.regex.Pattern.quote()
performs this wrapping between \\Q
and \\E
.