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发行说明。
The Pattern
API contains a number of useful predefined character classes, which offer convenient shorthands for commonly used regular expressions:Pattern
API包含许多有用的预定义字符类,它们为常用正则表达式提供了方便的简写:
. |
|
\d |
[0-9] |
\D |
[^0-9] |
\s |
[ \t\n\x0B\f\r] |
\S |
[^\s] |
\w |
[a-zA-Z_0-9] |
\W |
[^\w] |
In the table above, each construct in the left-hand column is shorthand for the character class in the right-hand column. 在上表中,左栏中的每个构造都是右栏中字符类的缩写。For example, 例如,\d
means a range of digits (0-9), and \w
means a word character (any lowercase letter, any uppercase letter, the underscore character, or any digit). \d
表示数字范围(0-9),而\w
表示单词字符(任何小写字母、任何大写字母、下划线字符或任何数字)。Use the predefined classes whenever possible. 尽可能使用预定义的类。They make your code easier to read and eliminate errors introduced by malformed character classes.它们使代码更易于阅读,并消除了由格式错误的字符类引入的错误。
Constructs beginning with a backslash are called escaped constructs. 以反斜杠开头的构造称为转义构造。We previewed escaped constructs in the String Literals section where we mentioned the use of backslash and 我们在字符串文字部分预览了转义构造,其中提到使用反斜杠和\Q
and \E
for quotation. \Q
和\E
作为引号。If you are using an escaped construct within a string literal, you must precede the backslash with another backslash for the string to compile. 如果在字符串文字中使用转义构造,则必须在反斜杠之前加上另一个反斜杠,才能编译该字符串。For example:例如:
private final String REGEX = "\\d"; // a single digit
In this example 在本例中\d
is the regular expression; the extra backslash is required for the code to compile. \d
是正则表达式;编译代码需要额外的反斜杠。The test harness reads the expressions directly from the 但是,测试线束直接从Console
, however, so the extra backslash is unnecessary.Console
读取表达式,因此不需要额外的反斜杠。
The following examples demonstrate the use of predefined character classes.以下示例演示预定义字符类的使用。
Enter your regex: . Enter input string to search: @ I found the text "@" starting at index 0 and ending at index 1. Enter your regex: . Enter input string to search: 1 I found the text "1" starting at index 0 and ending at index 1. Enter your regex: . Enter input string to search: a I found the text "a" starting at index 0 and ending at index 1. Enter your regex: \d Enter input string to search: 1 I found the text "1" starting at index 0 and ending at index 1. Enter your regex: \d Enter input string to search: a No match found. Enter your regex: \D Enter input string to search: 1 No match found. Enter your regex: \D Enter input string to search: a I found the text "a" starting at index 0 and ending at index 1. Enter your regex: \s Enter input string to search: I found the text " " starting at index 0 and ending at index 1. Enter your regex: \s Enter input string to search: a No match found. Enter your regex: \S Enter input string to search: No match found. Enter your regex: \S Enter input string to search: a I found the text "a" starting at index 0 and ending at index 1. Enter your regex: \w Enter input string to search: a I found the text "a" starting at index 0 and ending at index 1. Enter your regex: \w Enter input string to search: ! No match found. Enter your regex: \W Enter input string to search: a No match found. Enter your regex: \W Enter input string to search: ! I found the text "!" starting at index 0 and ending at index 1.
In the first three examples, the regular expression is simply 在前三个示例中,正则表达式是简单的.
(the "dot" metacharacter) that indicates "any character." .
(表示“任意字符”的“点”元字符)Therefore, the match is successful in all three cases (a randomly selected 因此,匹配在所有三种情况下都是成功的(随机选择的@
character, a digit, and a letter). @
字符、数字和字母)。The remaining examples each use a single regular expression construct from the Predefined Character Classes table. 其余的示例都使用预定义字符类表中的单个正则表达式构造。You can refer to this table to figure out the logic behind each match:您可以参考此表了解每个匹配背后的逻辑:
\d
\s
\w
Alternatively, a capital letter means the opposite:或者,大写字母表示相反的意思:
\D
\S
\W