预定义字符类
预定义字符类
The Pattern
API 包含许多有用的预定义字符类,它们为常用的正则表达式提供了便捷的简写形式
结构 | 描述 |
---|---|
. |
任何字符(可能匹配或不匹配行终止符) |
\d |
一个数字: [0-9] |
\D |
一个非数字: [^0-9] |
\s |
一个空白字符: [ \t\n\x0B\f\r] |
\S |
一个非空白字符: [^\s] |
\w |
一个单词字符: [a-zA-Z_0-9] |
\W |
一个非单词字符: [^\w] |
在上表中,左侧列中的每个结构都是右侧列中字符类的简写形式。例如,\d
表示一个数字范围(0-9),\w
表示一个单词字符(任何小写字母、任何大写字母、下划线字符或任何数字)。尽可能使用预定义的类。它们使您的代码更易于阅读,并消除由格式错误的字符类引入的错误。
以反斜杠开头的结构称为转义结构。我们在 字符串字面量
部分中预览了转义结构,其中我们提到了反斜杠和 \Q
和 \E
用于引用的用法。如果您在字符串字面量中使用转义结构,则必须在反斜杠之前添加另一个反斜杠才能使字符串编译。例如
private final String REGEX = "\\d"; // a single digit
在此示例中,\d
是正则表达式;代码编译需要额外的反斜杠。但是,测试工具直接从控制台读取表达式,因此额外的反斜杠是不必要的。
示例
以下示例演示了预定义字符类的用法。
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.
在前三个示例中,正则表达式只是 .
(“点”元字符),表示“任何字符”。因此,在所有三种情况下(随机选择的 @
字符、一个数字和一个字母)匹配都成功。其余示例每个都使用来自 预定义字符类 表的一个正则表达式结构。您可以参考此表来弄清楚每个匹配背后的逻辑
\d
匹配所有数字\s
匹配空格\w
匹配单词字符
或者,大写字母表示相反的意思
\D
匹配非数字\S
匹配非空格\W
匹配非单词字符
上次更新: 2022 年 1 月 10 日