Character Classes

如果浏览Pattern类规范,您将看到汇总支持的正则表达式结构的表。在“字符类”部分,您将找到以下内容:

ConstructDescription
[abc]a,b 或 c(简单类)
[^abc]除 a,b 或 c(取反)之外的任何字符
[a-zA-Z]a 到 z 或 A 到 Z(含)(范围)
[a-d[m-p]]a 到 d,或 m 到 p:a-dm-p
[a-z&&[def]]d,e 或 f(交叉点)
[a-z&&[^bc]]a 到 z,b 和 c 除外:ad-z
[a-z&&[^m-p]]a 到 z,而不是 m 到 p:a-lq-z

左列指定正则表达式构造,而右列描述每个构造将匹配的条件。

Note:

短语“字符类”中的单词“类”不表示.class文件。在正则表达式的上下文中,字符类是一组用方括号括起来的字符。它指定将成功匹配给定 Importing 字符串 中的单个字符的字符。

Simple Classes

字符类的最基本形式是简单地将一组字符并排放置在方括号内。例如,正则表达式[bcr]at将匹配单词“ bat”,“ cat”或“ rat”,因为它定义了一个字符类(接受“ b”,“ c”或“ r”)作为其第一个字符。

Enter your regex: [bcr]at
Enter input string to search: bat
I found the text "bat" starting at index 0 and ending at index 3.

Enter your regex: [bcr]at
Enter input string to search: cat
I found the text "cat" starting at index 0 and ending at index 3.

Enter your regex: [bcr]at
Enter input string to search: rat
I found the text "rat" starting at index 0 and ending at index 3.

Enter your regex: [bcr]at
Enter input string to search: hat
No match found.

在上述示例中,仅当第一个字母与字符类定义的字符之一匹配时,总体匹配才会成功。

Negation

要匹配列出的字符以外的所有字符,请在字符类的开头插入“ ^”元字符。这种技术被称为否定。

Enter your regex: [^bcr]at
Enter input string to search: bat
No match found.

Enter your regex: [^bcr]at
Enter input string to search: cat
No match found.

Enter your regex: [^bcr]at
Enter input string to search: rat
No match found.

Enter your regex: [^bcr]at
Enter input string to search: hat
I found the text "hat" starting at index 0 and ending at index 3.

仅当 Importing 字符串 的第一个字符不包含由字符类定义的任何字符时,匹配才会成功。

Ranges

有时,您可能需要定义一个包含一定范围值的字符类,例如字母“ a 至 h”或数字“ 1 至 5”。要指定范围,只需在要匹配的第一个和最后一个字符之间插入“ -”元字符,例如[1-5][a-h]。您还可以在类中将不同的范围并排放置,以进一步扩大 match 的可能性。例如,[a-zA-Z]将匹配字母表中的任何字母:a 到 z(小写)或 A 到 Z(大写)。

以下是范围和取反的一些示例:

Enter your regex: [a-c]
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.

Enter your regex: [a-c]
Enter input string to search: b
I found the text "b" starting at index 0 and ending at index 1.

Enter your regex: [a-c]
Enter input string to search: c
I found the text "c" starting at index 0 and ending at index 1.

Enter your regex: [a-c]
Enter input string to search: d
No match found.

Enter your regex: foo[1-5]
Enter input string to search: foo1
I found the text "foo1" starting at index 0 and ending at index 4.

Enter your regex: foo[1-5]
Enter input string to search: foo5
I found the text "foo5" starting at index 0 and ending at index 4.

Enter your regex: foo[1-5]
Enter input string to search: foo6
No match found.

Enter your regex: foo[^1-5]
Enter input string to search: foo1
No match found.

Enter your regex: foo[^1-5]
Enter input string to search: foo6
I found the text "foo6" starting at index 0 and ending at index 4.

Unions

您还可以使用并集来创建由两个或多个单独的字符类组成的单个字符类。要创建联合,只需将一个类嵌套在另一个类中,例如[0-4[6-8]]。这个特殊的联合会创建一个与数字 0、1、2、3、4、6、7 和 8 匹配的单个字符类。

Enter your regex: [0-4[6-8]]
Enter input string to search: 0
I found the text "0" starting at index 0 and ending at index 1.

Enter your regex: [0-4[6-8]]
Enter input string to search: 5
No match found.

Enter your regex: [0-4[6-8]]
Enter input string to search: 6
I found the text "6" starting at index 0 and ending at index 1.

Enter your regex: [0-4[6-8]]
Enter input string to search: 8
I found the text "8" starting at index 0 and ending at index 1.

Enter your regex: [0-4[6-8]]
Enter input string to search: 9
No match found.

Intersections

要创建仅与所有嵌套类的通用字符匹配的单个字符类,请使用&&,如[0-9&&[345]]一样。此特定的交集创建一个仅与两个字符类共有的数字匹配的字符类:3、4 和 5.

Enter your regex: [0-9&&[345]]
Enter input string to search: 3
I found the text "3" starting at index 0 and ending at index 1.

Enter your regex: [0-9&&[345]]
Enter input string to search: 4
I found the text "4" starting at index 0 and ending at index 1.

Enter your regex: [0-9&&[345]]
Enter input string to search: 5
I found the text "5" starting at index 0 and ending at index 1.

Enter your regex: [0-9&&[345]]
Enter input string to search: 2
No match found.

Enter your regex: [0-9&&[345]]
Enter input string to search: 6
No match found.

这是显示两个范围的交集的示例:

Enter your regex: [2-8&&[4-6]]
Enter input string to search: 3
No match found.

Enter your regex: [2-8&&[4-6]]
Enter input string to search: 4
I found the text "4" starting at index 0 and ending at index 1.

Enter your regex: [2-8&&[4-6]]
Enter input string to search: 5
I found the text "5" starting at index 0 and ending at index 1.

Enter your regex: [2-8&&[4-6]]
Enter input string to search: 6
I found the text "6" starting at index 0 and ending at index 1.

Enter your regex: [2-8&&[4-6]]
Enter input string to search: 7
No match found.

Subtraction

最后,您可以使用减法来否定一个或多个嵌套字符类,例如[0-9&&[^345]]。本示例创建一个单个字符类,该字符类匹配从 0 到 9 的所有字符,数字 3、4 和 5 除外。

Enter your regex: [0-9&&[^345]]
Enter input string to search: 2
I found the text "2" starting at index 0 and ending at index 1.

Enter your regex: [0-9&&[^345]]
Enter input string to search: 3
No match found.

Enter your regex: [0-9&&[^345]]
Enter input string to search: 4
No match found.

Enter your regex: [0-9&&[^345]]
Enter input string to search: 5
No match found.

Enter your regex: [0-9&&[^345]]
Enter input string to search: 6
I found the text "6" starting at index 0 and ending at index 1.

Enter your regex: [0-9&&[^345]]
Enter input string to search: 9
I found the text "9" starting at index 0 and ending at index 1.

既然我们已经介绍了如何创建字符类,那么您可能需要先阅读角色类别表,然后再 continue 下一节。