Fix “Illegal Repetition” PatternSyntaxException in Java
In Java, regular expressions (regex) offer powerful string matching capabilities via the java.util.regex package. However, writing incorrect regex patterns can lead to a common runtime error: java.util.regex.PatternSyntaxException: Illegal repetition near index This exception can confuse many developers, especially when the regex appears visually correct. Let us delve into understanding the java.util.regex.PatternSyntaxException, specifically the “illegal repetition near index” error, which commonly occurs when writing malformed regex patterns in Java.
1. Understanding the PatternSyntaxException in Java
In Java, the PatternSyntaxException is a runtime exception thrown by the Pattern.compile() method when the provided regular expression has invalid syntax. One of the most common messages associated with this exception is: Illegal repetition near index X. This specific message indicates that a repetition quantifier—such as * (zero or more), + (one or more), ? (zero or one), or a range quantifier like {n,m}—has been used incorrectly. The “index X” refers to the exact character position in the regex string where the problem was detected. These quantifiers must follow a valid character or group. When used at the start of a pattern or without a preceding token, they result in an invalid regex, triggering this exception.
2. Explaining the Exception with a Practical Example
This basic Java example demonstrates what happens when a regular expression contains a syntax error, specifically an illegal use of a quantifier without a preceding token. The pattern "*abc" is invalid because the asterisk (*) is a quantifier that requires a character or group to operate on. Since there is nothing before the asterisk, the compiler doesn’t know what to repeat, leading to a PatternSyntaxException.
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String[] args) {
String pattern = "*abc"; // Invalid pattern
Pattern.compile(pattern); // Will throw PatternSyntaxException
}
}
When this code is executed, the following error is thrown and printed to the console:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0 *abc ^
The error message clearly states that there is a “dangling meta character ‘*'” at index 0, pointing out that the asterisk is not valid at the start of the pattern. This highlights how important it is to understand the correct placement and usage of regex quantifiers. Proper syntax would be something like "a*bc", where * applies to the preceding character a.
3. What Triggers an “Illegal Repetition” Error in Regex?
Regex patterns must follow strict syntax rules. A PatternSyntaxException with the message “Illegal repetition” usually occurs due to one of the following:
- Quantifiers (
*,+,?,{}) used without a valid preceding token - Improperly escaped special characters
- Invalid or mismatched use of brackets, braces, or parentheses
3.1 Code Example
The following Java program showcases typical scenarios where a PatternSyntaxException is thrown due to incorrect use of quantifiers, repetition ranges, or grouping in regular expressions. This example helps illustrate how minor syntax mistakes in regex patterns can lead to runtime failures.
// RegexMistakesExample.java
import java.util.regex.Pattern;
public class RegexMistakesExample {
public static void main(String[] args) {
try {
Pattern.compile("*abc"); // Error: '*' cannot appear without a preceding token
} catch (Exception e) {
System.out.println("Error 1: " + e);
}
try {
Pattern.compile("a{3,2}"); // Error: Invalid repetition range (min > max)
} catch (Exception e) {
System.out.println("Error 2: " + e);
}
try {
Pattern.compile("file.*(txt|pdf"); // Error: Unclosed group
} catch (Exception e) {
System.out.println("Error 3: " + e);
}
}
}
The RegexMistakesExample class demonstrates three common mistakes that trigger the PatternSyntaxException with the message “Illegal repetition” in Java regular expressions. In the first try block, Pattern.compile("*abc") fails because the asterisk (*) quantifier is used without a valid preceding character to repeat, resulting in an illegal pattern. The second block, Pattern.compile("a{3,2}"), throws an exception because the repetition quantifier has an invalid range where the minimum (3) is greater than the maximum (2), which violates regex syntax rules. In the third example, Pattern.compile("file.*(txt|pdf") causes an error due to an unclosed group—the opening parenthesis for the capturing group (txt|pdf is not matched with a closing one. Each block catches the exception and prints a descriptive error message to help developers identify and correct the issue.
4. Best Practices
To prevent PatternSyntaxException errors such as “Illegal repetition,” follow these regex best practices during development:
- Always place repetition operators like
*,+,?, and{n,m}after a valid character or group. These quantifiers must follow a concrete element to repeat—never use them at the beginning of a pattern or on their own. - Escape meta characters such as
.,[,],{,},(,),|, and\when you want to treat them as literal characters. For example, to match a period, use"\\."in Java. - In Java, use double backslashes (
\) to properly escape regex symbols in string literals. For example,"\\d+"correctly represents one or more digits. A single backslash will result in a compilation error or unexpected behavior. - Test your regular expressions using online tools or regex validators like regex101.com, regexr.com, or IDE plugins. These tools provide syntax highlighting, error feedback, and match previews before integrating the regex into your codebase.
By incorporating these practices, developers can catch syntax errors early, write more maintainable patterns, and reduce runtime issues in production code.
5. Conclusion
The PatternSyntaxException: Illegal repetition near index error in Java regex typically results from incorrect use of quantifiers and meta characters. By ensuring patterns are well-formed and validated, you can avoid these runtime errors and write more reliable regex expressions. Always validate complex regex patterns using external tools or unit tests, and follow best practices like escaping special characters, using groups properly, and testing edge cases.

