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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。