The regular expression uses the “( )” round bracket to identify the matching groups in a string. The matched character in the round bracket identified as a match group in a string. The exception “java.util.regex.PatternSyntaxException: Unclosed group near index” will be thrown if the open round bracket is available without a close round bracket.



Exception

The stack trace of the exception java.util.regex.PatternSyntaxException: Unclosed group near index 1 is shown as below.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 1
(
 ^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.accept(Pattern.java:1813)
	at java.util.regex.Pattern.group0(Pattern.java:2908)
	at java.util.regex.Pattern.sequence(Pattern.java:2051)
	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

In the regular expression, the open round bracket is available without corresponding close round bracket. The open and close round bracket represents the group of the matched characters within a given string. The matched group output can be extracted separately using the group identifier.

If not, you try to match the open round bracket within the given string without using the proper escape characters.



How to reproduce this issue

The example below contains the regular expression with a open round bracket and without close round bracket.

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

Modify the regular expression in the above program with a close round bracket “)”. This will make a group that matches the characters in the specified string. The following example shows how to create a group using open and close round brackets in regular expression.

package com.yawintutor;

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

		str2 = str.replaceAll(pattern, "Clean$1");

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

Output

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


Solution 2

Modify the regular expression with escape characters for open round bracket. This escape character will match the open round bracket and replaces with empty string.

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 : Washington50)



Leave a Reply