java.util.regex.PatternSyntaxException: Illegal repetition

The java exception java.util.regex.PatternSyntaxException: Illegal repetition occurs when the regex dialects ‘{’ and ‘}’ are not used for repetition qualifier. In the regex dialect, the ‘{‘ and ‘}’ are special in java. They are the opening and closing tokens for the {m, n} repetition quantifier where m and n are integers. If these regular expression dialects are used for Illegal repetition, the error message java.util.regex.PatternSyntaxException: Illegal repetition will be thrown.

If the opening and closing tokens for the {m, n} are not used for repetition quantifier, you should escape them. Otherwise, it is considered as illegal repetition. The open and close tokens should be specified as “\\{name\\}”. If the opening and closing tokens are not used for repetition quantifier, the exception java.util.regex.PatternSyntaxException: Illegal repetition will be thrown.

In regular expression, the open token must end with a closing token. If the opening token is inserted without the closing token, the exception java.util.regex.PatternSyntaxException: Illegal repetition will be thrown.



Exception

The exception java.util.regex.PatternSyntaxException: Illegal repetition will be thrown as like the below stack trace.

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
{
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.closure(Pattern.java:3157)
	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 = "{";
		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: Illegal repetition
{
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.closure(Pattern.java:3157)
	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 *