The getMessage() method of the PatternSyntaxException class is used to retrieve a detailed error message describing why a regular expression is invalid. The returned message includes the error description, the index where the error occurred, the invalid regular expression, and a visual indicator (^) pointing to the error location.
- Helps quickly identify and debug invalid regex patterns.
- Commonly used inside a catch (PatternSyntaxException e) block.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Geeks {
public static void main(String[] args) {
String regex = "[";
String input = "GeeksforGeeks";
String replacement = "Hello";
try {
// Compile the regular expression
Pattern pattern = Pattern.compile(regex);
// Create a Matcher object
Matcher matcher = pattern.matcher(input);
// Replace all matches
input = matcher.replaceAll(replacement);
} catch (PatternSyntaxException e) {
// Print the detailed error message
System.out.println("Message:\n"
+ e.getMessage());
}
}
}
Output
Message: Unclosed character class near index 0 [ ^
Explanation: The regular expression "[" is invalid because the character class is not closed with a matching ]. When Pattern.compile() attempts to compile the pattern, it throws a PatternSyntaxException. The getMessage() method returns a detailed error message containing the error description, the index where the error occurred, the invalid pattern, and a caret (^) indicating the exact error location.
Syntax
public String getMessage()
Parameters: This method does not accept any parameters.
Return Value: This method returns a multi-line string containing:
- A description of the syntax error.
- The index where the error occurred.
- The invalid regular expression.
- A visual indicator (^) showing the error position.
Program: Using getMessage() with an Invalid Quantifier
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Geeks {
public static void main(String[] args) {
String regex = "{";
String input = "GeeksforGeeks";
String replacement = "Java";
try {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
input = matcher.replaceAll(replacement);
} catch (PatternSyntaxException e) {
System.out.println("Message:\n"
+ e.getMessage());
}
}
}
Output
Message:
Illegal repetition near index 1
{
Explanation: The regular expression "{" is invalid because it begins a repetition quantifier without a preceding expression. Therefore, Pattern.compile() throws a PatternSyntaxException. The getMessage() method returns a detailed message describing the error along with the invalid pattern and the position of the error.
Program: Using getMessage() with an Unclosed Group
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Geeks {
public static void main(String[] args) {
String regex = "(";
try {
Pattern.compile(regex);
} catch (PatternSyntaxException e) {
System.out.println("Message:\n"
+ e.getMessage());
}
}
}
Output
Message: Unclosed group near index 1 (
Explanation: The regular expression "(" starts a capturing group but does not include the required closing parenthesis ). As a result, Pattern.compile() throws a PatternSyntaxException. The getMessage() method returns a detailed error message indicating the reason for the error and its exact location.
Advantages of getMessage() Method
- Returns a detailed explanation of the regex syntax error.
- Displays the exact index where the error occurred.
- Shows the invalid regular expression along with a visual error indicator.
- Makes debugging regular expressions easier.
- Commonly used with getPattern(), getDescription(), and getIndex() for comprehensive exception handling.
- Useful for logging and displaying informative error messages in regex-based applications.