Open In App

Java PatternSyntaxException Class getPattern() Method with Examples

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 getPattern() Method

This method retrieves erroneous regular-expression patterns. This method can be invoked on the instantiated object of the PatternSyntaxException class by using the below syntax.

Syntax:

public String getPattern()

Return Type: This method returns a string, i.e., an erroneous pattern.

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

Java




// Java program to illustrate the 
// working of getPattern() 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 erroneous of the
            // Regular expression pattern
            System.out.println("Pattern: "
                               + e.getPattern());
        }
    }
}


Output

Pattern: [

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 getPattern()
// 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 erroneous of the
            // Regular expression pattern
            System.out.println("Pattern: "
                               + e.getPattern());
        }
    }
}


Output

Pattern: {


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