2. Character Classes

Matching specific sets of characters

In the previous article, we saw that the dot . matches any character. That's often too broad. Character classes let you specify exactly which characters are acceptable in a given position.

Square-Bracket Classes

A character class is written inside square brackets [...]. It matches one character from the set.

Pattern

c[aou]t

Input

cat cot cut cet cit

Matches

cat cot cut cet cit

[aou] means "one character that is a, o, or u." The class matches exactly one character — it doesn't mean "match all three."

Ranges

Instead of listing every character, you can specify a range with a hyphen:

Class Equivalent to Matches
[a-z] [abcdefghijklmnopqrstuvwxyz] Any lowercase letter
[A-Z] [ABCDEF...Z] Any uppercase letter
[0-9] [0123456789] Any digit
[a-zA-Z] Any letter (upper or lower)
[a-zA-Z0-9] Any alphanumeric character

Pattern

[A-Z][a-z]+

Input

Hello World 123 foo Bar

Matches

Hello World 123 foo Bar

[A-Z] matches one uppercase letter, then [a-z]+ matches one or more lowercase letters. Together they find capitalized words.

Combining Ranges and Literals

You can mix ranges and individual characters freely inside one class:

Pattern

[a-zA-Z_][a-zA-Z0-9_]*

What it matches

A valid identifier in most programming languages — starts with a letter or underscore, followed by zero or more letters, digits, or underscores.

Negated Classes

Put a caret ^ immediately after the opening bracket to negate the class — it matches any character not in the set.

Pattern

[^0-9]

Input

Room 404

Matches

Room 404

[^0-9] matches every character that is not a digit — including letters, spaces, and punctuation.

Important: the caret only means "negate" when it's the first character inside the brackets. Anywhere else, it's just a literal ^.

Shorthand Character Classes

Regex engines provide built-in shorthands for the most common classes:

Shorthand Equivalent Matches
\d [0-9] Any digit
\D [^0-9] Any non-digit
\w [a-zA-Z0-9_] Any "word" character
\W [^a-zA-Z0-9_] Any non-word character
\s [ \t\n\r\f\v] Any whitespace
\S [^ \t\n\r\f\v] Any non-whitespace

The uppercase versions are always the negation of the lowercase versions.

Pattern

\d{3}-\d{4}

Input

Call 555-1234 today

Matches

Call 555-1234 today

Shorthands Inside Classes

You can use shorthands inside square brackets to build compound classes:

Pattern

[\d\s]

What it matches

Any character that is either a digit or whitespace.

Pattern

[^\d\s]

What it matches

Any character that is neither a digit nor whitespace — effectively letters and punctuation.

Special Characters Inside Classes

Most metacharacters lose their special meaning inside [...]. Only a few remain special:

Character Special inside [...]? How to include literally
] Yes — closes the class \] or put it first: []abc]
\ Yes — escape character \\
^ Yes — negation (only if first) Put it anywhere but first: [a^b]
- Yes — range separator \- or put it first/last: [-abc] or [abc-]

Everything else — ., *, +, ?, (, ), {, }, | — is just a literal character inside a class. No escaping needed.

Pattern

[.?!]

What it matches

A period, question mark, or exclamation point. No escaping required — inside [...] they're all literal.

POSIX Character Classes

Some regex flavors (notably POSIX-compatible ones like grep and awk) support named classes written as [:name:] inside brackets:

POSIX class Equivalent Matches
[:alpha:] [a-zA-Z] Letters
[:digit:] [0-9] Digits
[:alnum:] [a-zA-Z0-9] Letters and digits
[:space:] [ \t\n\r\f\v] Whitespace
[:upper:] [A-Z] Uppercase letters
[:lower:] [a-z] Lowercase letters
[:punct:] Punctuation characters

These must be used inside an outer set of brackets: [[:alpha:]]. They're locale-aware, which means they can handle accented characters in some environments — something [a-z] cannot.

Key Takeaways

  • [abc] matches exactly one character from the set.
  • Ranges like [a-z] and [0-9] cover contiguous character ranges.
  • [^...] negates the class — matches anything not listed.
  • \d, \w, \s (and their uppercase negations) are convenient shorthands.
  • Most metacharacters are literal inside [...] — only ] \ ^ - need care.

Next up: quantifiers — how to specify how many times a character or group should repeat.