Documentation

The Java™ Tutorials
Hide TOC
Introduction介绍
Trail: Essential Java Classes
Lesson: Regular Expressions

Introduction介绍

What Are Regular Expressions?什么是正则表达式?

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中的最相似。

How Are Regular Expressions Represented in This Package?正则表达式在这个包中是如何表示的?

The java.util.regex package primarily consists of three classes: Pattern, Matcher, and PatternSyntaxException.java.util.regex包主要由三个类组成:PatternMatcherPatternSyntaxException

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.因此,下一节将介绍一个简单的测试工具,它将被反复使用以探索它们的语法。


Previous page: Regular Expressions
Next page: Test Harness