If you need to match characters that are a part of the regular expression syntax you can mark all or part of the pattern as a regex literal.
\Q marks the beginning of the regex literal.
\E marks the end of the regex literal.
// the following throws a PatternSyntaxException because of the un-closed bracket "[123".matches("[123"); // wrapping the bracket in \Q and \E allows the pattern to match as you would expect. "[123".matches("\\Q[\\E123"); // returns true
An easier way of doing it without having to remember the
\E escape sequences is to use
"[123".matches(Pattern.quote("[") + "123"); // returns true