📜  Java PatternSyntaxException 类 getIndex() 方法和示例

📅  最后修改于: 2022-05-13 01:54:24.142000             🧑  作者: Mango

Java PatternSyntaxException 类 getIndex() 方法和示例

Java PatternSyntaxException 类在Java.util.regex包下定义,它描述了未经检查的异常,表示正则表达式模式中的语法错误。可以使用以下语法声明此类,

public class PatternSyntaxException extends IllegalArgumentException

PatternSyntaxException::getIndex()

此方法用于检索作为异常抛出的错误的索引。语法如下,

句法:

public int getIndex()

返回:它返回一个非负整数:如果无法检索到索引,则错误模式中的索引为 -1

示例 1:在此示例中,错误位于模式中的索引 0 处。这是因为未闭合的字符。

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());
      }
    }
}


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());
      }
    }
}


输出
Index: 0
Message: Unclosed character class near index 0
[
^

示例 2:在此示例中, getIndex()方法返回 -1。这是因为非法重复索引无法检索。

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());
      }
    }
}
输出
Index: -1
Message: Illegal repetition
{