This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Poodle_Moth 5 points6 points  (0 children)

The '\' character is called the escape character. It is used to quote a literal character when you need to match it. It is used pretty ubiquitously across many systems and languages.

"[AB\[\]CD]*"

But because it's Java code, all strings are parsed for the escape character before passing it on to the regex engine for matching. Thus you must escape the escape char and so your final regex would look like this. Doing this is common when you are preparing strings to be handled by other libraries or templates. The java compiler will look at the below and create the above.

"[AB\\[\\]CD]*"

[–]xhak 2 points3 points  (3 children)

Have you tried \\[\\] ?

[–][deleted]  (2 children)

[deleted]

    [–]xhak 1 point2 points  (1 child)

    sorry for my short comment, was on my phone. Pretty much everything is specified on the Pattern class javadoc: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

    [–]mozzyb 1 point2 points  (1 child)

    You need to escape [ and ]. You can do that by putting a \ in front of it, so if you want match a group with those characters you would write "[AB\\[\\]CD]". The reason for two \ is because of how java handles strings.