VOOZH about

URL: https://www.geeksforgeeks.org/java/java-patternsyntaxexception-class-getpattern-method-with-examples/

⇱ Java PatternSyntaxException Class getPattern() Method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java PatternSyntaxException Class getPattern() Method with Examples

Last Updated : 23 Jul, 2025

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.


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.


Output
Pattern: {
Comment