RegexBuilder
← Back to the tools
🔤

Character Classes in Regex: [a-z], \d, \w and \s Explained

A character class matches exactly one character, chosen from a set. [abc] matches a, b or c. Classes are how you say "any vowel", "any digit" or "any character except a comma".

Square brackets mean "one of these"

[aeiou] matches one vowel. [0-9] matches one digit. [a-zA-Z] matches one letter of either case. The dash creates a range; a comma or a period inside the brackets are literal.

Negated classes flip the meaning

A caret right after the opening bracket inverts the class: [^0-9] matches any character that is not a digit. It is often the cleanest way to say "anything except".

Shorthand classes save typing

\d is short for [0-9], \w for [A-Za-z0-9_] (word characters), and \s for any whitespace. Their uppercase twins invert them: \D, \W, \S.

Combining classes and quantifiers

Classes combine naturally with quantifiers: \d{3} is three digits, [a-z]+ is one or more lowercase letters, \s* is any amount of whitespace. Together they build almost every practical pattern.

Escape a dash or bracket when it is special

Inside a class, a dash between two characters makes a range, and a closing bracket ends the class. To match them literally, escape with a backslash (\-, \]) or place them where they cannot form a range.

Test and explain patterns free