Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unclosed Character Class Error?

Tags:

java

regex

split

Here is the error:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 3
], [
   ^
    at java.util.regex.Pattern.error(Pattern.java:1924)
    at java.util.regex.Pattern.clazz(Pattern.java:2493)
    at java.util.regex.Pattern.sequence(Pattern.java:2030)
    at java.util.regex.Pattern.expr(Pattern.java:1964)
    at java.util.regex.Pattern.compile(Pattern.java:1665)
    at java.util.regex.Pattern.<init>(Pattern.java:1337)
    at java.util.regex.Pattern.compile(Pattern.java:1022)
    at java.lang.String.split(String.java:2313)
    at java.lang.String.split(String.java:2355)
    at testJunior2013.J2.main(J2.java:31)

This is the area of the code that is causing the issues.

String[][] split = new String[1][rows];

split[0] = (Arrays.deepToString(array2d)).split("], ["); //split at the end of an array row

What does this error mean and what needs to be done to fix the code above?

like image 335
ylun.ca Avatar asked Feb 16 '14 21:02

ylun.ca


People also ask

What does unclosed character literal mean in Java?

Unclosed Character Literal. Means, you started with ' single quote, so compiler just expects a single character after opening ' and then a closing ' . Hence, the character literal is considered unclosed and you see the error. So, either you use char data type and ' single quotes to enclose single character.

What is Java util regex PatternSyntaxException?

The java. util. regex. PatternSyntaxException class represents a unchecked exception thrown to indicate a syntax error in a regular-expression pattern.


2 Answers

TL;DR

You want:

.split("\\], \\[")`

Escape each square bracket twice — once for each context in which you need to strip them from their special meaning: within a Regular Expression first, and within a Java String secondly.

Consider using Pattern#quote when you need your entire pattern to be interpreted literally.


Explanation

String#split works with a Regular Expression but [ and ] are not standard characters, regex-wise: they have a special meaning in that context.

In order to strip them from their special meaning and simply match actual square brackets, they need to be escaped, which is done by preceding each with a backslash — that is, using \[ and \].

However, in a Java String, \ is not a standard character either, and needs to be escaped as well.

Thus, just to split on [, the String used is "\\[" and you are trying to obtain:

.split("\\], \\[")

A sensible alternative

However, in this case, you're not just semantically escaping a few specific characters in a Regular Expression, but actually wishing that your entire pattern be interpreted literally: there's a method to do just that 🙂

Pattern#quote is used to signify that the:

Metacharacters [...] in your pattern will be given no special meaning.

(from the Javadoc linked above)

I recommend, in this case, that you use the following, more sensible and readable:

.split(Pattern.quote("], ["))
like image 97
ccjmne Avatar answered Oct 18 '22 00:10

ccjmne


Split receives a regex and [, ] characters have meaning in regex, so you should escape them with \\[ and \\].

The way you are currently doing it, the parser finds a ] without a preceding [ so it throws that error.

like image 6
xp500 Avatar answered Oct 18 '22 00:10

xp500