Skip to content

What is a regex character class? Brackets, ranges, and shorthand

Sep 16, 2026 · Tooling

A regex character class is a set of characters enclosed in square brackets that matches exactly one character from that set at a single position in the string. For example, [aeiou] matches any one vowel. Character classes are one of the most fundamental regex building blocks, letting you express choices compactly, define ranges, negate sets, and use shorthand for common groups like digits and word characters. Getting them right is central to writing precise, bug-free patterns.

Key takeaways

  • A character class in square brackets matches exactly one character from the set, not a sequence.
  • Ranges like [a-z] and [0-9] match any single character within the range.
  • A leading caret negates the class, so [^0-9] matches any non-digit character.
  • Shorthand classes such as d, w, and s stand for common sets and have negated forms.
  • Most metacharacters lose their special meaning inside a class, so a dot there is a literal dot.
  • Hyphen and caret placement matters: position them carefully to treat them as literals.

What does a character class match?

A character class matches a single character that is a member of the set. The brackets describe options for one position, so [cbr]at matches cat, bat, or rat, but never two of those letters at once. This is the key mental model: the class collapses several alternatives for one character into a compact expression.

Because it consumes exactly one character, a class must be paired with a quantifier to match more. For instance, [0-9] matches one digit, while [0-9]+ matches one or more consecutive digits. Confusing the two is a frequent beginner error.

How do character ranges work?

Inside a class, a hyphen between two characters defines a range based on their underlying code point order. Common ranges include:

  • [a-z] matches any lowercase ASCII letter.
  • [A-Z] matches any uppercase ASCII letter.
  • [0-9] matches any digit.
  • [a-zA-Z0-9] matches any alphanumeric character.

You can combine ranges and individual characters in one class, as in [a-fA-F0-9] for a hexadecimal digit. Be careful with ranges that span across character categories, such as [A-z], which unintentionally includes several punctuation characters that sit between uppercase and lowercase letters in the ASCII table. Prefer explicit, intentional ranges.

How do I negate a character class?

Placing a caret ^ as the very first character inside the brackets negates the set, matching any single character that is not listed. So [^0-9] matches any non-digit, and [^aeiou] matches any character that is not a lowercase vowel.

Two important nuances:

  • The caret only negates when it is first. Elsewhere, as in [a^b], it is a literal caret.
  • A negated class still matches exactly one character, and it will match line breaks or other characters you might not expect unless you account for them.

Negated classes are powerful for validation, but think about edge cases: [^0-9] matches whitespace, punctuation, and letters alike, which may be broader than intended.

What are the shorthand character classes?

Regex engines provide shorthand escapes for frequently used sets, along with their negations. These make patterns shorter and clearer.

Shorthand Matches Negated form Negation matches
d A digit D A non-digit
w A word character (letters, digits, underscore) W A non-word character
s A whitespace character S A non-whitespace character

You can mix shorthand into a bracketed class, as in [w.-] to match a word character, dot, or hyphen. Keep in mind that the exact definitions of w and d can vary between engines and Unicode modes; in Unicode-aware engines they may match beyond ASCII, so test against your target inputs.

Which characters need escaping inside a class?

A pleasant feature of character classes is that most metacharacters lose their special meaning inside the brackets. A dot is literal, so [.] matches only a period, and a star or plus inside a class matches those literal symbols. This is often cleaner than escaping with a backslash.

A few characters still need attention:

  • Closing bracket ] must be escaped or placed where it cannot end the class.
  • Backslash must be escaped as \.
  • Caret ^ is special only as the first character; place it elsewhere to keep it literal.
  • Hyphen - is a range operator between two characters; put it first or last to treat it literally.

For example, [-.] matches a hyphen or a dot because the hyphen is first, and [a-z-] matches lowercase letters or a literal hyphen because the second hyphen is last.

What are POSIX and Unicode character classes?

Beyond ASCII ranges, some engines support named classes for portability and Unicode awareness. POSIX bracket expressions use the form [[:name:]] inside a class, such as [[:alpha:]] for letters or [[:digit:]] for digits. They read clearly and avoid hardcoding ranges.

Unicode-aware engines also offer property escapes, commonly written as p{...}, to match by category. For instance, a pattern may match any letter across scripts rather than just ASCII letters. Support and exact syntax vary by language and engine, so:

  • Check whether your engine supports POSIX classes or Unicode properties before relying on them.
  • Enable the appropriate Unicode flag when you need matching beyond ASCII.
  • Prefer named classes over long manual ranges when they are available, for readability.

What are common character class mistakes?

Several pitfalls trip up developers writing character classes:

  • Forgetting a class matches one character: writing [abc] expecting the whole word; add a quantifier such as [abc]+.
  • Unintended ranges: a stray hyphen in the middle creates a range, so [a-z-A] may not mean what you think. Anchor the hyphen at an end.
  • Over-broad negation: [^,] matches newlines and everything else except a comma, which can span more than a single field.
  • Assuming ASCII: d and w may match Unicode characters in some engines, breaking naive validation.
  • Redundant escaping: escaping a dot inside a class as . works but is unnecessary and can obscure intent.

When in doubt, test your class against representative inputs including edge cases like empty strings, whitespace, and non-ASCII characters, since regex behavior is easy to misjudge by reading alone.

How do character classes combine with quantifiers and anchors?

Because a class matches a single character, real patterns pair it with quantifiers to describe length and with anchors to describe position. This is where character classes become genuinely useful for validation and extraction.

  • [0-9]+ matches one or more digits, useful for whole numbers.
  • [a-z]* matches zero or more lowercase letters.
  • [A-Za-z0-9_]{3,16} matches a bounded run, such as a username length rule.

Anchors tie the class to the start or end of the string. A pattern like ^[a-z][a-z0-9]*$ means the whole string must start with a lowercase letter followed by any number of lowercase letters or digits, and nothing else. Without the anchors, the same pattern would match merely a portion of the input, which is a frequent cause of validation bugs where invalid strings slip through because only a substring matched.

Consider a simple example that extracts hex color values:

#[0-9a-fA-F]{6}

Here the class defines the allowed characters, the quantifier fixes the count at six, and the literal # anchors the shape. Building patterns this way, one class and one quantifier at a time, keeps them readable and correct.

Frequently asked questions

What is a regex character class?

A character class is a set of characters written in square brackets that matches exactly one character from the set at a given position.

What is the difference between [abc] and (abc)?

Square brackets match one character that is a, b, or c, while parentheses group the literal sequence abc and also capture it.

How do I negate a character class?

Place a caret as the first character inside the brackets, so [^0-9] matches any single character that is not a digit.

What does d mean in regex?

The shorthand d matches a single digit. In many engines it can match Unicode digits, though it is commonly used for ASCII 0 through 9.

Do I need to escape a dot inside a character class?

No. Inside a character class a dot is a literal period, so [.] matches a literal dot without escaping.

How do I include a hyphen in a character class?

Put the hyphen first or last inside the brackets, or escape it, so it is treated literally rather than as a range operator.

What is the difference between a character class and a quantifier?

A character class defines what one character may be, while a quantifier like plus or star controls how many times the preceding element repeats.

Frequently asked questions

What is a regex character class?

A character class is a set of characters written in square brackets that matches exactly one character from the set at a given position.

What is the difference between [abc] and (abc)?

Square brackets match one character that is a, b, or c, while parentheses group the literal sequence abc and also capture it.

How do I negate a character class?

Place a caret as the first character inside the brackets, so [^0-9] matches any single character that is not a digit.

What does \d mean in regex?

The shorthand \d matches a single digit. In many engines it can match Unicode digits, though it is commonly used for ASCII 0 through 9.

Do I need to escape a dot inside a character class?

No. Inside a character class a dot is a literal period, so [.] matches a literal dot without escaping.

How do I include a hyphen in a character class?

Put the hyphen first or last inside the brackets, or escape it, so it is treated literally rather than as a range operator.

What is the difference between a character class and a quantifier?

A character class defines what one character may be, while a quantifier like plus or star controls how many times the preceding element repeats.

Contact: contact@quikconsolecom.net