The java exception java.util.regex.PatternSyntaxException: Unmatched closing ‘)’ happens when the string is matched by the regular expression special character ‘)’ closing parentheses. In a search string, the character “(” and “)” and “{” and “}” are special characters in regular expression. these dialect characters have to be escaped. You need to escape ‘(‘ and ‘)’ with ‘\\(‘ and ‘\\)’

The regular expression dialect uses the “( )” round bracket to identify the matching groups in a string. The matched character in the round bracket has been identified as a match group in a string. If the regular expression dialect closing token “)” is used without opening token, the exception java.util.regex.PatternSyntaxException: Unclosed group near index will be thrown. If the regular expression tries to match the opening and closing character of the token, the dialect must be escaped.



Exception

The java exception java.util.regex.PatternSyntaxException: Unmatched closing ‘)’ will show as like below stack trace. The java Pattern class will throw the error.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing ')'
)
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.compile(Pattern.java:1700)
	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 close round bracket “)” is added in the regular expression without having open round bracket. Either the regular expression group is created using open round bracket “(” along with close round bracket “)” or the close round bracket should be escaped to match the character.



How to reproduce this issue

The regular expression must have the opening and closing tokens to match the group of characters. Otherwise, the opening and closing token must be escaped to match the character. The example below contains regular expression with a close round bracket and without open round bracket. The exception java.util.regex.PatternSyntaxException: Unclosed group near index will be thrown as there are no opening token in the regular expression.

package com.yawintutor;

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

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

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


Solution 1

If the regular expression attempts to match the closing token as a character, the closing character must be added with the escape character as seen in the example below. If an escape character is added to the closing token, the regular expression will not be processed as a group dialect.

package com.yawintutor;

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

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

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

Output

Given  String : Washington(50)
Output String : Washington(50


Solution 2

If the regular expression attempts to match the closing token of a group of characters, the opening token must be inserted before the closing token. The regular expression processes the opening and closing token as a group dialect and tries to match the group of characters as specified.

package com.yawintutor;

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

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

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

Output

Given  String : Washington(50)
Output String : Washington



Leave a Reply