Use regex 字符 classes 有效
字符 classes [...] 匹配任何 one 字符从 集合. Learn ranges, negation, shorthand classes, 和 escaping rules.
- 1
字符类 [abc] matches , b, 或 c. Ranges: [a-z] matches 任何 lowercase letter. Negation: [^0-9] matches 任何 non-数字. Shorthand: \d (digits), \w (字 chars), \s (whitespace), 和其 uppercase negations \D, \W, \S. 内部 [], 最多特殊 characters lose 其 meaning — 仅 ^, -, ], 和 \ need escaping.
- 2 正则表达式测试器 →
Test 字符类 patterns 针对样本文本. Try [a-zA-Z] 为 letters, [0-9a-fA-F] 为 hex digits, [^\s] 为 non-whitespace. tool shows 所有 matches, 哪个 helps 验证模式 matches 什么你 expect.
- 3 正则表达式替换器 →
Use 字符 classes 在 replacements. 替换 [0-9] 具有 * 到 mask digits, 或 [aeiou] 具有 _ 到 strip vowels. 字符 classes 在 replacement 是字面量 — 它们不 need escaping.
常见问题
- 什么是 差集之间 \d 和 [0-9]?
- 在最多 regex flavors, 它们是 identical. However, 在一些 Unicode-aware flavors (Java,。NET 具有 RegexOptions), \d matches 所有 Unicode 数字 characters (e.g., Arabic-Indic digits ٠-٩), while [0-9] matches 仅 ASCII digits. Use [0-9] 为 ASCII-仅 matching.
- I need 到 escape 特殊 characters 内部 []?
- 最多特殊 characters (. * + ? { } ( ) |) 是字面量内部字符 classes — 否 escaping needed. Only ^ (如果第一, 为 negation), - (为 ranges, 或 escape 或 put 第一/最后), ] (到包含它, escape 或 put 第一), 和 \ (到 escape) need 特殊 handling.