Utiliser regex caractère classes effectively
Caractère classes [...] correspondre any one caractère de un définir. Apprendre ranges, negation, shorthand classes, et escaping règles.
- 1
A character class [abc] matches a, b, or c. Ranges: [a-z] matches any lowercase letter. Negation: [^0-9] matches any non-digit. Shorthand: \d (digits), \w (word chars), \s (whitespace), and their uppercase negations \D, \W, \S. Inside [], most special characters lose their meaning — only ^, -, ], and \ need escaping.
- 2 Testeur de regex →
Test character class patterns against sample text. Try [a-zA-Z] for letters, [0-9a-fA-F] for hex digits, [^\s] for non-whitespace. The tool shows all matches, which helps verify the pattern matches what you expect.
- 3 Remplacement par regex →
Use character classes in replacements. Replace [0-9] with * to mask digits, or [aeiou] with _ to strip vowels. Character classes in replacement are literal — they do not need escaping.
Questions fréquentes
- What is the difference between \d and [0-9]?
- In most regex flavors, they are identical. However, in some Unicode-aware flavors (Java, .NET with RegexOptions), \d matches all Unicode digit characters (e.g., Arabic-Indic digits ٠-٩), while [0-9] matches only ASCII digits. Use [0-9] for ASCII-only matching.
- Do I need to escape special characters inside []?
- Most special characters (. * + ? { } ( ) |) are literal inside character classes — no escaping needed. Only ^ (if first, for negation), - (for ranges, or escape or put first/last), ] (to include it, escape or put first), and \ (to escape) need special handling.