[Solved] PatternSyntaxException: Illegal Repetition when using regex in Java

In this post, I will be sharing how to fix java.util.regex.PatternSyntaxException: Illegal repetition in Java with examples. java.util.regex.PatternSyntaxException is an unchecked exception. According to Java docs,  this exception is thrown to indicate a syntax error in a regular-expression pattern. Let's dive deep into the topic:

Read Also: [Solved] java.util.regex.PatternSyntaxException: dangling meta character '+' near index 0

[Fixed] PatternSyntaxException: Illegal Repetition in Java

As always, first, we will produce the java.util.regex.PatternSyntaxException: illegal repetition in java before moving on to the solution.

Example 1: Producing the exception by not escaping the opening and closing curly braces


You will get this exception when you are trying not to escape the opening and closing curly braces as shown below in the example:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.PatternSyntaxException;

public class IllegalRepetition 
{
    
private static String REGEX = "{\"user_id\" : [0-9]*}";
public static void main(String args[]) { try { Pattern pattern = Pattern.compile(REGEX); String inputString = "Alive is Awesome"; Matcher matcher = pattern.matcher(inputString); inputString = matcher.replaceAll("LoveYourself"); } catch (PatternSyntaxException e) { System.out.println("PatternSyntaxException: " + "Message: "+ e.getMessage() + "Pattern: "+ e.getPattern()); } } }

Output:
PatternSyntaxException: Message: Illegal repetition near index 1
{"user_id" : [0-9]*}
 ^Pattern: {"user_id" : [0-9]*}


Explanation


{ and } have special meanings when using regex in Java. In simple words, { is an indicator to the regex pattern that you are about to start a repetition indicator, like {3,6} which means '3 to 6 times of the previous token'. But {user_id is illegal, because it has to be followed by a number, so it throws an exception.

Solution


The simple solution to the above problem is you need to escape all regex meta-characters (in this case { and }) as shown below in the example:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class IllegalRepetition 
{
    
private static String REGEX = "\\{\"user_id\" : [0-9]*\\}";
public static void main(String args[]) { try { Pattern pattern = Pattern.compile(REGEX); String inputString = "Alive is Awesome"; Matcher matcher = pattern.matcher(inputString); inputString = matcher.replaceAll("LoveYourself"); } catch (PatternSyntaxException e) { System.out.println("PatternSyntaxException: " + "Message: "+ e.getMessage() + "Pattern: "+ e.getPattern()); } } }


That's all for today. Please mention in the comments in case you are still facing the java.util.regex.PatternSyntaxException: Illegal Repetition when using regex in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry