Java PatternSyntaxException Class getIndex() Method with Examples Last Updated : 02 Feb, 2022 Comments Improve Suggest changes Like Article Like Report 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 IllegalArgumentExceptionPatternSyntaxException::getIndex() This method is used to retrieve the index of the error which is thrown as an exception. The syntax is given below, Syntax: public int getIndex() Returns: It returns a non-negative integer: The index in the pattern of the error is -1 if the index cannot be retrieved Example 1: In this example, the error is at the index 0 in the pattern. This is because of the unclosed character. 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 index in the error of the pattern System.out.println("Index: "+ e.getIndex()); System.out.println("Message: "+ e.getMessage()); } } } OutputIndex: 0 Message: Unclosed character class near index 0 [ ^ Example 2: In this example, getIndex() method returns -1. This is because of illegal repetition the index cannot be retrieved. 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("Index: "+ e.getIndex()); System.out.println("Message: "+ e.getMessage()); } } } OutputIndex: -1 Message: Illegal repetition { Comment More infoAdvertise with us Next Article Java PatternSyntaxException Class getIndex() Method with Examples bhuwanesh Follow Improve Article Tags : Java Java-Functions Java-PatternSyntaxException-Class Practice Tags : Java 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 getPattern() 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 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 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 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 PatternSyntaxException Class in Java Java uses the java.util.regex API for pattern matching with Regular expressions. To indicate any syntax errors in a regular-expression pattern, Java provides a class called PatternSyntaxException. PatternSyntaxException Class in Java: While writing regular expressions, to identify and throw any synt 6 min read StringCharacterIterator getIndex() method in Java with Examples The getIndex() method of java.text.StringCharacterIterator class in Java is used to get the current index that is to be read by this StringCharacterIterator. This method returns that index number. Syntax: public int getIndex() Parameter: This method do not accept any parameter. Return Value: This me 1 min read Class getClasses() method in Java with Examples The getClasses() method of java.lang.Class class is used to get the classes of this class, which are the class and interfaces that are public and its members. The method returns the classes of this class in the form of array of Class objects. Syntax: public Class[] getClasses() Parameter: This metho 2 min read StringCharacterIterator getEndIndex() method in Java with Examples The getEndIndex() method of java.text.StringCharacterIterator class in Java is used to get the index at the ending that is to be read by this StringCharacterIterator. This method returns that index number. Syntax: public int getEndIndex() Parameter: This method do not accept any parameter. Return Va 1 min read Class getInterfaces() method in Java with Examples The getInterfaces() method of java.lang.Class class is used to get the interfaces directly implemented by this entity. This entity can be a class or an interface. The method returns an array of interfaces directly implemented by this entity.Syntax: public Class<T>[] getInterfaces() Parameter: 2 min read Like