Java PatternSyntaxException Class getPattern() Method with Examples
Last Updated :
02 Feb, 2022
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());
}
}
}
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());
}
}
}
Similar Reads
Java PatternSyntaxException Class getMessage() Method with Examples
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. Th
4 min read
Java PatternSyntaxException Class getIndex() Method with Examples
Java PatternSyntaxException Class is defined under the java.util.regex package and it depicts unchecked exception that signifies syntax error in a regular expression pattern. This class can be declared using the below syntax, public class PatternSyntaxException extends IllegalArgumentExceptionPatter
2 min read
Java PatternSyntaxException Class getDescription() Method with Examples
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 IllegalArgume
4 min read
Class getPackage() method in Java with Examples
The getPackage() method of java.lang.Class class is used to get the package of this entity. This entity can be a class, an array, an interface, etc. The method returns the package of this entity.Syntax: public Package getPackage() Parameter: This method does not accept any parameter.Return Value: Th
1 min read
Class getName() method in Java with Examples
The getName() method of java.lang.Class class is used to get the name of this entity. This entity can be a class, an array, an interface, etc. The method returns the name of the entity as a String.Syntax: public String getName() Parameter: This method does not accept any parameter.Return Value: This
1 min read
ParsePosition getIndex() method in Java with Example
The getIndex() method of java.text.ParsePosition class is used to retrieve the current parse position of this ParsePositon object . Syntax: public int getIndex() Parameter: This method does not accepts any argument as parameter. Return Value: This method returns the current parse position of this pa
2 min read
Class getTypeName() method in Java with Examples
The getTypeName() method of java.lang.Class class is used to get the type name of this class, which provides the information about this class' type. The method returns the type name of this class in the form of String. Syntax: public String getTypeName() Parameter: This method does not accept any pa
2 min read
ParsePosition getErrorIndex() method in Java with Example
The getErrorIndex() method of java.text.ParsePosition class is used to retrieve the index at which parse error may occur. Syntax: public int getErrorIndex() Parameter: This method does not accepts any argument as a parameter. Return Value: This method returns the index at which parse error may occur
2 min read
Class getTypeParameters() method in Java with Examples
The getTypeParameters() method of java.lang.Class class is used to get the type parameters of this entity. This entity can be a class, an array, an interface, etc. The method returns an array of TypeVariable objects representing the type variables.Syntax: public TypeVariable<Class<T>> ge
2 min read
Class getSigners() method in Java with Examples
The getSigners() method of java.lang.Class class is used to get the signers of this class. The method returns the signers of this class. If this object represents a primitive type or void, then this method returns null. Syntax: public Object[] getSigners() Parameter: This method does not accept any
2 min read