# 字符类

> 原文： [https://docs.oracle.com/javase/tutorial/essential/regex/char_classes.html](https://docs.oracle.com/javase/tutorial/essential/regex/char_classes.html)

如果浏览 [`Pattern`](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html) 类规范，您将看到总结支持的正则表达式结构的表。在“字符类”部分中，您将找到以下内容：

| 构造 | 描述 |
| --- | --- |
| `[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:** The word "class" in the phrase "character class" does not refer to a `.class` file. In the context of regular expressions, a _character class_ is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string.

* * *

## 简单的类

字符类的最基本形式是简单地将一组字符并排放在方括号内。例如，正则表达式`[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.

```

在上面的示例中，只有当第一个字母与字符类定义的字符之一匹配时，整体匹配才会成功。

### 否定

要匹配除以外的所有字符 _，请在字符类的开头插入“`^`”元字符。这种技术称为 _ 否定 _。_

```

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.

```

仅当输入字符串的第一个字符 _ 而不是 _ 包含字符类定义的任何字符时，匹配才会成功。

### 范围

有时您需要定义一个包含一系列值的字符类，例如字母“a 到 h”或数字“1 到 5”。要指定范围，只需在要匹配的第一个和最后一个字符之间插入“`-`”元字符，例如`[1-5]`或`[a-h]`。您还可以在类中将不同的范围放在彼此旁边，以进一步扩展匹配的可能性。例如，`[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.

```

### 工会

您还可以使用 _ 联合 _ 创建由两个或多个单独的字符类组成的单个字符类。要创建联合，只需将一个类嵌套在另一个类中，例如`[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.

```

### 交叉口

要创建仅匹配其所有嵌套类共有的字符的单个字符类，请使用`[0-9&&[345]]`，如`[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.

```

### 减法

最后，您可以使用 _ 减法 _ 来否定一个或多个嵌套字符类，例如`[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.

```

现在我们已经介绍了如何创建字符类，您可能需要在继续下一部分之前查看[字符类表](#CHART)。