The java exception java.util.regex.PatternSyntaxException: Unclosed character class near index 0 happens when the string is matched by the regular expression special character ‘[’ open square bracket. In a search string, the special character (open square bracket) must escape.

The regular expression uses the “[ ]” square bracket to match one of the characters with a character in a string. If any character in the square bracket matches the given string, it will be true. The exception “java.util.regex.PatternSyntaxException: Unclosed character class near index” will be thrown if the open square bracket is available without a close square bracket.



Exception

The stack trace of the exception java.util.regex.PatternSyntaxException: Unclosed character class near index 0 will be shown as below

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.clazz(Pattern.java:2548)
	at java.util.regex.Pattern.sequence(Pattern.java:2063)
	at java.util.regex.Pattern.expr(Pattern.java:1996)
	at java.util.regex.Pattern.compile(Pattern.java:1696)
	at java.util.regex.Pattern.<init>(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at java.lang.String.replaceAll(String.java:2223)
	at com.yawintutor.StringReplaceAll.main(StringReplaceAll.java:9)


Root Cause

The “[ ]” square bracket is used to group characters to match one of the characters in a given String. In the regular expression, the open square bracket “[‘ is added without a close square bracket “]”.

If not, you try to match open square bracket “[” without using proper escape character.



How to recreate this issue

Construct a regular expression with a open square bracket and without close square bracket or without any escape characters. The following example throws the exception.

package com.yawintutor;

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Bangalore[50]";
		String pattern = "[";
		String str2;

		str2 = str.replaceAll(pattern, "");

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}


Solution 1

Create the regular expression with close square bracket. The regular expression matches one of the characters listed inside square brackets. In the example below, it substitutes empty space for the occurrences of “a” and “g.”

package com.yawintutor;

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Bangalore[50]";
		String pattern = "[ag]";
		String str2;

		str2 = str.replaceAll(pattern, "");

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Output

Given  String : Bangalore[50]
Output String : Bnlore[50]


Solution 2

Use the escape characters before the open square bracket in the regular expression. The open square bracket “[” is replaced by an empty string in the example below.

package com.yawintutor;

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Bangalore[50]";
		String pattern = "\\[";
		String str2;

		str2 = str.replaceAll(pattern, "");

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Output

Given  String : Bangalore[50]
Output String : Bangalore50]



Leave a Reply