The getPattern() method of the PatternSyntaxException class is used to retrieve the invalid regular expression pattern that caused a PatternSyntaxException to be thrown. It is commonly used while handling regex syntax errors to identify the incorrect pattern and simplify debugging.
- Returns the invalid regular expression that caused the exception.
- Helps identify and debug incorrect 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 invalid regex pattern
System.out.println("Invalid Pattern: "
+ e.getPattern());
}
}
}
Output
Invalid Pattern: [
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 getPattern() method returns the invalid regular expression that caused the exception.
Syntax
public String getPattern()
- Parameters: This method does not accept any parameters.
- Return Value: This method returns the invalid regular expression pattern that caused the PatternSyntaxException.
Program: Using getPattern() 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("Invalid Pattern: "
+ e.getPattern());
}
}
}
Output
Invalid Pattern: {
Explanation: The regular expression "{" is invalid because it starts a quantifier without a valid expression. Therefore, Pattern.compile() throws a PatternSyntaxException, and the getPattern() method returns the invalid pattern.
Program: Using getPattern() 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("Invalid Pattern: "
+ e.getPattern());
}
}
}
Output
Invalid Pattern: (
Explanation: The regular expression "(" starts a capturing group but does not include the required closing parenthesis ). As a result, Pattern.compile() throws a PatternSyntaxException, and getPattern() returns the invalid regular expression.
Common Methods of PatternSyntaxException
| Method | Description |
|---|---|
| getDescription() | Returns a description of the syntax error. |
| getPattern() | Returns the invalid regular expression pattern. |
| getIndex() | Returns the index where the syntax error occurred. |
| getMessage() | Returns a complete formatted error message. |
Advantages of getPattern() Method
- Returns the exact regular expression that caused the exception.
- Helps debug invalid regex patterns quickly.
- Improves error reporting in regex-based applications.
- Commonly used with getDescription() and getIndex() for detailed exception handling.
- Useful while validating user-entered regular expressions.