Visual regex workbench

Build, test, explain and master regex with visual tools, guides, examples and a growing regex library.

Regex guide

Regex Character Classes

Character classes are one of the foundations of regular expressions. They let you match digits, letters, whitespace, punctuation, custom sets of characters and everything except a given set.

Why character classes matter

Character classes are one of the most frequently used building blocks in regular expressions. Instead of matching one exact character, they describe categories of characters such as digits, letters, whitespace or custom sets.

They allow regexes to remain short, readable and flexible. A single shortcut like \d replaces ten possible characters, while a custom class such as [A-F0-9] clearly describes hexadecimal digits.

Almost every practical regex relies on character classes, whether you are validating an email address, extracting numbers, parsing logs or cleaning user input.

Shortcut classes versus custom classes

Regex engines provide convenient shortcuts such as \d, \w and \s, but they are only predefined character classes.

Whenever these shortcuts are not specific enough, you can build your own classes with square brackets.

Choosing between a shortcut and a custom class is often a trade-off between readability and precision.

ASCII versus Unicode

One of the biggest sources of confusion is that character classes do not always behave the same with international text.

Depending on the regex engine and flags, shortcuts such as \w may only match ASCII characters or may include accented letters and many other scripts.

Whenever your application handles multilingual text, verify Unicode support instead of assuming every engine behaves identically.

ASCII versus Unicode

One of the biggest sources of confusion is that character classes do not always behave the same with international text.

Depending on the regex engine and flags, shortcuts such as \w may only match ASCII characters or may include accented letters and many other scripts.

Whenever your application handles multilingual text, verify Unicode support instead of assuming every engine behaves identically.

Character classes are the foundation of most regexes

If you look at real-world regexes, most of them are built from character classes combined with quantifiers and groups.

Understanding character classes thoroughly makes almost every other regex concept easier to learn.

For beginners, mastering this topic provides one of the highest returns on time invested.

Choosing the right character class

Several character classes may seem suitable for the same task, but they do not always produce identical results.

For example, \d and [0-9] are often equivalent, while \w may behave differently from [A-Za-z0-9_] depending on the regex engine and Unicode support.

Choosing the most appropriate character class improves readability, reduces ambiguity and helps produce more predictable regexes across different environments.

Continue learning

📘 Recommended guide

Regex Compatibility

Character classes can behave differently across regex engines, especially with Unicode, \w, \d and case-insensitive matching.

Read the complete guide →
📘 Recommended guide

Testing Regex

Character classes should be tested with valid characters, rejected characters, edge cases and international input when relevant.

Read the complete guide →
📘 Recommended guide

Common Regex Mistakes

Many regex bugs come from misunderstanding what shortcuts like \w, \d or . really match.

Read the complete guide →
📘 Recommended guide

Quantifiers

Character classes define what can match, while quantifiers define how many times it can repeat.

Together, they form the foundation of most practical regular expressions.

Read the complete guide →

Dot wildcard

The dot . matches almost any single character.

By default, many regex engines do not let . match line breaks unless a dot-all flag or mode is enabled.

.
Quick test

Digits with \d

The shortcut \d matches a digit.

It is commonly used for numbers, dates, identifiers, phone numbers and numeric validation.

\d+
Quick test

Non-digits with \D

The shortcut \D matches any character that is not a digit.

It is the opposite of \d.

\D+
Quick test

Word characters with \w

The shortcut \w usually matches letters, digits and underscore.

Its exact Unicode behavior depends on the regex engine and flags.

\w+
Quick test

Non-word characters with \W

The shortcut \W matches characters that are not word characters.

It is useful for finding separators, punctuation or symbols.

\W+
Quick test

Whitespace with \s

The shortcut \s matches whitespace characters.

This usually includes spaces, tabs and line breaks.

\s+
Quick test

Non-whitespace with \S

The shortcut \S matches any character that is not whitespace.

It is often used to capture visible text chunks.

\S+
Quick test

Custom character classes

Square brackets let you define your own character set.

For example, [aeiou] matches one lowercase vowel.

[aeiou]
Quick test

Character ranges

Inside a character class, a hyphen can define a range.

For example, [a-z] matches lowercase ASCII letters and [0-9] matches ASCII digits.

[a-z0-9]+
Quick test

Negated character classes

A caret at the start of a character class negates it.

For example, [^0-9] matches any character that is not an ASCII digit.

[^0-9]+
Quick test