Three letters that save you typing
The most common character classes have shorthand forms. They mean exactly what they look like:
\d— a digit. ASCII:[0-9]. Unicode-mode: any Unicode digit.\w— a "word" character. ASCII:[A-Za-z0-9_]. Unicode-mode: letters + digits + connectors.\s— a whitespace character.[ \t\n\r\f\v], plus Unicode whitespace in Unicode mode.
These are universal — every regex flavor supports them.
Unicode-aware vs ASCII-only
This is where flavors diverge subtly:
- Python 3:
\d,\w,\sdefault to Unicode-aware.\dmatches Arabic-Indic digits, Devanagari digits, etc. Use there.ASCIIflag to force ASCII-only. - JavaScript: ASCII-only by default. The
uflag enables Unicode but doesn't change\wto include Unicode letters. Use\p{L}withuflag for that. - RE2 / Go / ripgrep: ASCII by default. Use
\p{Nd},\pLetc for Unicode categories. - PCRE: ASCII by default;
(*UCP)or/umode enables Unicode-aware.
The \w trap
\w includes underscore but NOT hyphen. So \w+ matches my_var but not my-var. People constantly forget this. If you want "identifier-like with hyphens," use [\w-]+ explicitly.