java.util.regex.PatternSyntaxException: Unclosed counted closure near index

The java exception java.util.regex.PatternSyntaxException: Unclosed counted closure near index occurs when a string matches by the regular expression special character ‘{’ open curly bracket without close curly bracket. In a search string, the special character (close curly bracket) must be added.

The regular expression uses the “{ }” curly bracket to fit a group of characters in a string with a character. The number within the curly bracket shows the minimum number of character occurrences. The second value specifies the maximum character occurrence number. If the open curly bracket is available without a close curly bracket, the exception java.util.regex.PatternSyntaxException: Unclosed counted closure near index will be thrown.



Exception

The exception java.util.regex.PatternSyntaxException: Unclosed counted closure near index will be thrown as like the below stack trace.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed counted closure near index 2
{1
  ^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.closure(Pattern.java:3141)
	at java.util.regex.Pattern.sequence(Pattern.java:2134)
	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)


How to recreate this error

In the regular expression, add a open curly bracket without close curly bracket. Create a string that includes curly brackets. If the regular expression tries to match the string, the matching curly brackets should be returned. Since the curly bracket does not end with the closed bracket, this will create an error.

package com.yawintutor;

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

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

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

Output

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed counted closure near index 2
{1
  ^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.closure(Pattern.java:3141)
	at java.util.regex.Pattern.sequence(Pattern.java:2134)
	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 “{}” curly bracket is used to group characters within a given String. In the regular expression, the open curly bracket “{‘ is added without a close curly bracket “}”. All curly brackets that are open must be finished with a curly bracket closing. If the closed curly bracket is skipped, the group can not be defined by a regular expression.



Solution 1

Use an open and close curly bracket to create a regular expression. The character sequence in the given string will be identified by this. Within the curly bracket, the numbers specified indicate the minimum and maximum number of occurrences.

package com.yawintutor;

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Bangalore{1}";
		String pattern = "a{1,2}";
		String str2;

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

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

Output

Given  String : Bangalore{1}
Output String : Bnglore{1}


Solution 2

To match the curly bracket within the given string, use the escape characters before the open curly bracket in the regular expression. In the example below, the open curly bracket “{” is replaced by an empty string.

package com.yawintutor;

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

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

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

Output

Given  String : Bangalore{1}
Output String : Bangalore}



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *