The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
A PatternSyntaxException
is an unchecked exception that indicates a syntax error in a regular expression pattern. PatternSyntaxException
是一种未经检查的异常,它指示正则表达式模式中的语法错误。The PatternSyntaxException
class provides the following methods to help you determine what went wrong:PatternSyntaxException
类提供了以下方法来帮助您确定出错的原因:
public String getDescription()
public int getIndex()
public String getPattern()
public String getMessage()
The following source code, 以下源代码RegexTestHarness2.java
, updates our test harness to check for malformed regular expressions:RegexTestHarness2.java
更新了我们的测试工具,以检查格式不正确的正则表达式:
import java.io.Console; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.regex.PatternSyntaxException; public class RegexTestHarness2 { public static void main(String[] args){ Pattern pattern = null; Matcher matcher = null; Console console = System.console(); if (console == null) { System.err.println("No console."); System.exit(1); } while (true) { try{ pattern = Pattern.compile(console.readLine("%nEnter your regex: ")); matcher = pattern.matcher(console.readLine("Enter input string to search: ")); } catch(PatternSyntaxException pse){ console.format("There is a problem" + " with the regular expression!%n"); console.format("The pattern in question is: %s%n", pse.getPattern()); console.format("The description is: %s%n", pse.getDescription()); console.format("The message is: %s%n", pse.getMessage()); console.format("The index is: %s%n", pse.getIndex()); System.exit(0); } boolean found = false; while (matcher.find()) { console.format("I found the text" + " \"%s\" starting at " + "index %d and ending at index %d.%n", matcher.group(), matcher.start(), matcher.end()); found = true; } if(!found){ console.format("No match found.%n"); } } } }
To run this test, enter 要运行此测试,请输入?i)foo
as the regular expression. ?i)foo
作为正则表达式。This mistake is a common scenario in which the programmer has forgotten the opening parenthesis in the embedded flag expression 这种错误是程序员忘记嵌入标志表达式(?i)
. (?i)
中的左括号的常见情况。Doing so will produce the following results:这样做将产生以下结果:
Enter your regex: ?i) There is a problem with the regular expression! The pattern in question is: ?i) The description is: Dangling meta character '?' The message is: Dangling meta character '?' near index 0 ?i) ^ The index is: 0
From this output, we can see that the syntax error is a dangling metacharacter (the question mark) at index 0. 从这个输出中,我们可以看到语法错误是索引0处的悬空元字符(问号)。A missing opening parenthesis is the culprit.一个缺少的括号是罪魁祸首。