Open In App

Java PatternSyntaxException Class getMessage() Method with Examples

Last Updated : 13 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Java provides Regular Expressions or Regex (in short) API for creating string patterns to search, manipulate, and edit a string in Java. This class is defined under the java.util.regex package and it focuses upon the unchecked exception that signifies syntax error in a regular expression pattern. This class can be declared using the below syntax,

Syntax:

public class PatternSyntaxException extends IllegalArgumentException

This class has the following constructor:

PatternSyntaxException(String desc, String regex, int index) // It instantiates a new object of this class

PatternSyntaxException getMessage() Method

This method returns a multi-line string that contains a detailed description of the syntax error in a regular expression pattern and its index, the error in the regular-expression pattern. It also describes the indication of the error-index within the pattern in a visual format. This method can be invoked on the instantiated object of the PatternSyntaxException class by using the below syntax.

Syntax:

error.getMessage()

Here, an error is an object of PatterSyntaxException class

Return Type: This method returns a Multi-line string, i.e., a detailed description of the error.

Example 1: In this example, we have defined a pattern by invoking compile() method of the Pattern class. It accepts regularExpression (“[“) as an argument and generates a pattern. Then, we are calling the matcher() method on the instantiated object of the Pattern class. It accepts input as a parameter. This will check the regularExpresssion (pattern) in the input string. Moreover, we have called the replaceAll() method on the matcher object. This will replace every subsequence of the input sequence that would match with the pattern by the given replacement string.

Note that we have bundled these statements inside a Try block. In this case, it will be throwing a syntax error in the regular expression pattern and that is handled through catch block. The catch block accepts the object of the PatternSyntaxException class and eventually, we have invoked the getMessage() method on this object.

Java




// Java program to illustrate the
// working of getMessage() method
 
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
 
class GFG {
 
    private static String regularExpression = "[";
    private static String input
        = "GeeksforGeeks "
          + "is a learning platform.";
    private static String replace = "Hi";
 
    public static void main(String[] args)
    {
 
        try {
            // Compile the pattern
            Pattern pattern
                = Pattern.compile(regularExpression);
 
            // To get a matcher object
            Matcher matcher = pattern.matcher(input);
            input = matcher.replaceAll(replace);
        }
        catch (PatternSyntaxException e) {
 
            // Print the message of the error occurred in
            // the pattern
            System.out.println("Message: "
                               + e.getMessage());
        }
    }
}


 
 

Output

Message: Unclosed character class near index 0
[
^

 

Example 2: In this example, we have defined a pattern by invoking compile() method of the Pattern class. It accepts regularExpression (“{“) as an argument and generates a pattern. Then, we are calling the matcher() method on the instantiated object of the Pattern class. It accepts input as a parameter. This will check the regularExpresssion (pattern) in the input string which is equal to “Geeks ” + “is a learning platform.”. Moreover, we have called the replaceAll() method on the matcher object. This will replace every subsequence of the input sequence that would match with the pattern by the given replacement string.

 

Note that we have bundled these statements inside a Try block. In this case, it will be throwing a syntax error in the regular expression pattern and that is handled through catch block. The catch block accepts the object of the PatternSyntaxException class and eventually, we have invoked the getMessage() method on this object.

 

Java




// Java program to illustrate the
// working of getMessage() method
 
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
 
class GFG {
 
    // Declaring a string variable to store the regular
    // expression
    private static String regularExpression = "{";
    private static String input
        = "GeeksforGeeks "
          + "is a learning platform.";
    private static String replace = "Hello";
 
    public static void main(String[] args)
    {
 
        try {
 
            // Compile the pattern
            Pattern pattern
                = Pattern.compile(regularExpression);
 
            // To get a matcher object
            Matcher matcher = pattern.matcher(input);
            input = matcher.replaceAll(replace);
        }
        catch (PatternSyntaxException e) {
 
            // Print the message of the error occurred in
            // the pattern
            System.out.println("Message: "
                               + e.getMessage());
        }
    }
}


 
 

Output

Message: Illegal repetition
{

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads