Character Sets

Match any character in set.

[abc]       # Matches "a", "b", or "c"
[aeiou]     # Matches any vowel
[0-9]       # Matches any digit
[a-z]       # Matches lowercase letter
[A-Z]       # Matches uppercase letter
[a-zA-Z]    # Matches any letter

Ranges

Match character ranges.

[0-9]       # Digits 0 through 9
[a-f]       # Letters a through f
[A-F]       # Letters A through F
[a-z0-9]    # Lowercase letters and digits
[A-Za-z]    # All letters

Negated Classes

Match anything NOT in set.

[^abc]      # Anything except a, b, c
[^0-9]      # Any non-digit
[^a-z]      # Any non-lowercase letter
[^\s]       # Any non-whitespace

Shorthand Classes

Common character classes.

\d          # Digit [0-9]
\D          # Non-digit [^0-9]
\w          # Word char [a-zA-Z0-9_]
\W          # Non-word char
\s          # Whitespace [ \t\n\r\f\v]
\S          # Non-whitespace

Multiple Ranges

Combine multiple ranges.

[a-zA-Z0-9]     # Alphanumeric
[a-z0-9_-]      # Letters, digits, underscore, hyphen
[^a-zA-Z]       # Non-alphabetic

Special Characters in Sets

Most metacharacters are literal in [].

[.]         # Literal period
[*]         # Literal asterisk
[+]         # Literal plus
[?]         # Literal question mark
[\^]        # Literal caret (escaped)

Practical Examples

Real-world character class usage.

[0-9]{3}         # 3 digits
[a-z]+           # One or more lowercase letters
[A-Z][a-z]*      # Capitalized word
[^0-9]+          # Non-numeric characters

Common mistakes / Pitfalls

  • People often copy a command or pattern without adapting placeholders, which can break production workflows unexpectedly.
  • It is easy to forget environment-specific differences, so always verify behavior in your shell, runtime, or API gateway before shipping.
  • Many errors come from skipping small validation steps, so test with realistic sample input before relying on the result.
Last updated: February 2026