The java.util.regex.PatternSyntaxException: Unclosed character class near index 0 example shows the reasons for this exception. It also shows how to fix it using various solutions.
What is java.util.regex.PatternSyntaxException: Unclosed character class near index and what causes it?
The opening square bracket character “[” is a meta-character in the regular expression, which means it has a special meaning. It is used to specify the character class. If we specify the opening square bracket in the regex pattern to match it literally without escaping it, it causes this exception.
Reason 1: Trying to split a string by square bracket using the split method?
This exception will be thrown when you try to split the string by an opening square bracket using the split
method. Since the split
method accepts a regex pattern and the opening square bracket is a meta-character in regex, Java throws this exception as given below.
1 2 3 4 5 6 |
String str = "Total records found:[100]"; String[] parts = str.split("["); for(String part : parts) { System.out.println(part); } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 |
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^ at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.clazz(Unknown Source) at java.util.regex.Pattern.sequence(Unknown Source) at java.util.regex.Pattern.expr(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.<init>(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.lang.String.split(Unknown Source) at java.lang.String.split(Unknown Source) |
Solution:
We need to escape the “[” character in the split
method.
1 2 3 4 5 6 |
String str = "Total records found:[100]"; String[] parts = str.split("\\["); for(String part : parts) { System.out.println(part); } |
Output
1 2 |
Total records found: 100] |
You can also use the quote
method of the Pattern class instead of escaping the pattern as given below.
1 |
String[] parts = str.split(Pattern.quote("[")); |
Reason 2: Trying to replace square brackets using the replaceAll method?
Just like the split
method, the replaceAll
method also accepts the regex pattern.
1 2 3 4 |
String str = "[One],[Two],[Three]"; str = str.replaceAll("[", ""); System.out.println(str); |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^ at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.clazz(Unknown Source) at java.util.regex.Pattern.sequence(Unknown Source) at java.util.regex.Pattern.expr(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.<init>(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.lang.String.replaceAll(Unknown Source) |
Solution:
We need to escape all meta-characters we need to match literally in the replaceAll
method.
1 2 3 4 |
String str = "[One],[Two],[Three]"; str = str.replaceAll("\\[", ""); System.out.println(str); |
Output
1 |
One],Two],Three] |
Reason 3: Trying to mention the character class in the regex pattern?
You are trying to specify the character range to match but forgot to close the square bracket as given below. Like in the example given below, the pattern “[0-9” is missing the closing square bracket for the numeric character class.
1 2 3 4 5 6 |
Pattern pattern = Pattern.compile("[0-9"); Matcher matcher = pattern.matcher("Learn 100% Java regex"); while(matcher.find()) { System.out.println("Matched \"" + matcher.group() + "\""); } |
Output
1 2 3 4 5 6 7 8 9 10 |
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 3 [0-9 ^ at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.clazz(Unknown Source) at java.util.regex.Pattern.sequence(Unknown Source) at java.util.regex.Pattern.expr(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.<init>(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) |
Solution:
Make sure that you have a closing square bracket for every character class specified.
1 2 3 4 5 6 |
Pattern pattern = Pattern.compile("[0-9]"); Matcher matcher = pattern.matcher("Learn 100% Java regex"); while(matcher.find()) { System.out.println("Matched \"" + matcher.group() + "\""); } |
Output
1 2 3 |
Matched "1" Matched "0" Matched "0" |
This example is a part of the Java Regular Expression Tutorial with Examples.
Please let me know your views in the comments section below.