Negated Shorthand Classes
What you'll learn: How to use the inverse versions of shorthand character classes to match everything except digits, word characters, or whitespace.
The Flip Side of Shortcuts
You've already learned about \d, \w, and \s — the handy shortcuts for matching digits, word characters, and whitespace. Now meet their opposites: negated shorthand classes use uppercase letters to match the inverse of what their lowercase versions match.
Think of them as "not" versions:
\Dmatches anything that is not a digit (everything except 0-9)\Wmatches anything that is not a word character (everything except letters, digits, and underscores)\Smatches anything that is not whitespace (everything except spaces, tabs, newlines, etc.)
When to Use Them
These are perfect when you want to find "everything else." For example:
\Dfinds punctuation, letters, spaces — anything but numbers\Wfinds spaces, punctuation, symbols — anything but alphanumeric characters and underscores\Sfinds visible characters — anything but whitespace
They're particularly useful for validation patterns like "ensure there are no non-digit characters in this string" or "find the first non-whitespace character."
The Mental Model
Remember: lowercase = match these characters, uppercase = match everything else. The uppercase version is like flipping a switch from "allow" to "deny."
Key Takeaway: Use \D, \W, and \S to match the inverse of \d, \w, and \s — they're the "not" versions of the shorthand classes you already know.