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发行说明。
Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. 正则表达式是一种基于集合中每个字符串共享的公共特征来描述一组字符串的方法。They can be used to search, edit, or manipulate text and data. 它们可用于搜索、编辑或操作文本和数据。You must learn a specific syntax to create regular expressions one that goes beyond the normal syntax of the Java programming language. 您必须学习创建正则表达式的特定语法一种超越Java编程语言正常语法的语言。Regular expressions vary in complexity, but once you understand the basics of how they're constructed, you'll be able to decipher (or create) any regular expression.正则表达式的复杂程度各不相同,但一旦您了解了它们的基本构造方法,就可以破译(或创建)任何正则表达式。
This trail teaches the regular expression syntax supported by the 本教程介绍java.util.regex
API and presents several working examples to illustrate how the various objects interact. java.util.regex
API支持的正则表达式语法,并提供几个工作示例来说明各种对象如何交互。In the world of regular expressions, there are many different flavors to choose from, such as grep, Perl, Tcl, Python, PHP, and awk. 在正则表达式的世界中,有许多不同的风格可供选择,例如grep、Perl、Tcl、Python、PHP和awk。The regular expression syntax in the java.util.regex
API is most similar to that found in Perl.java.util.regex
API中的正则表达式语法与Perl中的最相似。
The java.util.regex
package primarily consists of three classes: Pattern
, Matcher
, and PatternSyntaxException
.java.util.regex
包主要由三个类组成:Pattern
、Matcher
和PatternSyntaxException
。
Pattern
object is a compiled representation of a regular expression. Pattern
对象是正则表达式的编译表示。Pattern
class provides no public constructors. Pattern
类不提供公共构造函数。public static compile
methods, which will then return a Pattern
object. public static compile
方法,然后该方法将返回一个Pattern
对象。Matcher
object is the engine that interprets the pattern and performs match operations against an input string. Matcher
对象是解释模式并对输入字符串执行匹配操作的引擎。Pattern
class, Matcher
defines no public constructors. Pattern
类一样,Matcher
不定义公共构造函数。Matcher
object by invoking the matcher
method on a Pattern
object.Pattern
对象上的matcher
方法来获得Matcher
对象。PatternSyntaxException
object is an unchecked exception that indicates a syntax error in a regular expression pattern.PatternSyntaxException
对象是未经检查的异常,它指示正则表达式模式中的语法错误。The last few lessons of this trail explore each class in detail. 本课程的最后几节课将详细探讨每门课程。But first, you must understand how regular expressions are actually constructed. 但首先,您必须了解正则表达式实际上是如何构造的。Therefore, the next section introduces a simple test harness that will be used repeatedly to explore their syntax.因此,下一节将介绍一个简单的测试工具,它将被反复使用以探索它们的语法。