Javaregex 简明教程
Java Regex - PatternSyntaxException Class
Class declaration
以下是 java.util.regex.PatternSyntaxException 类的声明 −
public class PatternSyntaxException
extends IllegalArgumentException
Constructors
Sr.No |
Method & Description |
1 |
*PatternSyntaxException(String desc, String regex, int index)*构造此类的新的实例。 |
Class methods
Sr.No |
Method & Description |
1 |
*String getDescription()*检索错误的描述。 |
2 |
*int getIndex()*检索错误索引。 |
3 |
*String getMessage()*返回一个多行字符串,其中包含语法错误及其索引、错误的正则表达式模式和模式中错误索引的可视指示。 |
4 |
*String getPattern()*检索错误的正则表达式模式。 |
Example
以下示例展示了 java.util.regex.Pattern.PatternSyntaxException 类方法的用法。
package com.tutorialspoint;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class PatternSyntaxExceptionDemo {
private static String REGEX = "[";
private static String INPUT = "The dog says meow " + "All dogs say meow.";
private static String REPLACE = "cat";
public static void main(String[] args) {
try{
Pattern pattern = Pattern.compile(REGEX);
// get a matcher object
Matcher matcher = pattern.matcher(INPUT);
INPUT = matcher.replaceAll(REPLACE);
} catch(PatternSyntaxException e){
System.out.println("PatternSyntaxException: ");
System.out.println("Description: "+ e.getDescription());
System.out.println("Index: "+ e.getIndex());
System.out.println("Message: "+ e.getMessage());
System.out.println("Pattern: "+ e.getPattern());
}
}
}
让我们编译并运行上述程序,这将生成以下结果 −
PatternSyntaxException:
Description: Unclosed character class
Index: 0
Message: Unclosed character class near index 0
[
^
Pattern: [