Open In App

Java PatternSyntaxException Class getDescription() Method with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Java PatternSyntaxException 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 getDescription() Method

Java provides Regular Expressions or Regex (in short) API for creating string patterns to search, manipulate, and edit a string in Java. This method is used to get the description of the error in a regular expression pattern. This method has the following syntax,

Syntax:

error.getDescription()

Here, the error is an object of PatterSyntaxException class

Return Type: The return type of this method is a string, i.e., a 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 getDescription() method on this object.

Java




// Java program to illustrate the working of getIndex()
// 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 description of the error occurred in
            // the pattern
            System.out.println("Description: "
                               + e.getDescription());
        }
    }
}


 
 

Output

Description: Unclosed character class

 

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 getDescription() method on this object.

 

Java




// Java program to illustrate the working of getIndex()
// 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 index in the error of the pattern
            System.out.println("Description: "
                               + e.getDescription());
        }
    }
}


 
 

Output

Description: Illegal repetition

 



Last Updated : 13 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads