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());
}
}
}
輸出
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 {
相關用法
- Java PatternSyntaxException getDescription()用法及代碼示例
- Java PatternSyntaxException getMessage()用法及代碼示例
- Java Pattern asPredicate()用法及代碼示例
- Java Pattern compile(String)用法及代碼示例
- Java Pattern flags()用法及代碼示例
- Java Pattern matcher(CharSequence)用法及代碼示例
- Java Pattern pattern()用法及代碼示例
- Java Pattern quote(String)用法及代碼示例
- Java Pattern splitAsStream()用法及代碼示例
- Java Pattern split(CharSequence)用法及代碼示例
- Java Pattern toString()用法及代碼示例
- Java Pattern split(CharSequence,int)用法及代碼示例
- Java Pattern matches(String ,CharSequence)用法及代碼示例
- Java Pattern compile(String,int)用法及代碼示例
- Java Pattern用法及代碼示例
- Java Path compareTo()用法及代碼示例
- Java Path endsWith()用法及代碼示例
- Java Path equals()用法及代碼示例
- Java Path getFileName()用法及代碼示例
- Java Path getFileSystem()用法及代碼示例
- Java Path getNameCount()用法及代碼示例
- Java Path getName(int)用法及代碼示例
- Java Path getParent()用法及代碼示例
- Java Path getRoot()用法及代碼示例
- Java Path hashCode()用法及代碼示例
注:本文由純淨天空篩選整理自bhuwanesh大神的英文原創作品 Java PatternSyntaxException Class getIndex() Method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。