The java.util.regex.PatternSyntaxException: Illegal repetition near index 0 exception occurs when the regular expression special characters such as $ { } are used inside the java search string. When processing in the regular expression, the special characters have significant meaning. It is necessary to add a special character with an escape to the search. If the special characters are used with out an escape character, the error java.util.regex.PatternSyntaxException: Illegal repetition near index 0 is thrown out.
The regular expression engine recognises the character { as it is about to start a repeat predictor, such as {1,5}, which is 1 to 5 times the previous token. For example, {a} is illegal as the a number must be followed by the character ‘{‘ , thus the exception Illegal repetition near index will be thrown.
The special characters such as $, {, }, [, ] has significant meaning in the regular expression. Curly brackets define the character group found in the given string. The $ determines the character’s end of the line. If the special characters must be escape to search the character. Otherwise the regular expression will throw exception.
Exception
The java exception java.util.regex.PatternSyntaxException: Illegal repetition near index 0 will display the stack trace as shown below.
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 0
${name}
^
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:10)
How to recreate this exception
In the regular expression, add substitute variable with $ and curly brackets { }. These characters have special meaning with in the regular expression. The search string containing the special characters will throw the exception as shown below.
package com.yawintutor;
public class StringReplaceAll {
public static void main(String[] args) {
String str = "Welcome ${name}";
String pattern = "${name}";
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 near index 0
${name}
^
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:10)
Root Cause
The special characters such as $ { } [ ] is used to identify a group of characters within a given String. If special characters are used to match characters in a string, Java attempts to validate them based on the unique function of the character that generates the exception. In order to search for a character in a given string, special characters must escape.
Solution 1
In order to search for a character in a given string, special characters must escape. The special characters in the regular expression will be searched as a character if it is marked with escape character. The example below shows how to add escape in the regular expression
package com.yawintutor;
public class StringReplaceAll {
public static void main(String[] args) {
String str = "Welcome ${name}";
String pattern = "\\$\\{name\\}";
String str2;
str2 = str.replaceAll(pattern, "");
System.out.println("Given String : " + str);
System.out.println("Output String : " + str2);
}
}
Output
Given String : Welcome ${name}
Output String : Welcome
Solution 2
The character { should be used to start a repeat predictor. A number must be followed by the character {. For example, {1,5} which is 1 to 5 times the previous token.
package com.yawintutor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringReplaceAll {
public static void main(String[] args) {
String str = "Welcome ${name}";
String pattern = "e{1,3}";
String str2;
str2 = str.replaceAll(pattern, "");
System.out.println("Given String : " + str);
System.out.println("Output String : " + str2);
}
}
Output
Given String : Welcome ${name}
Output String : Wlcom ${nam}