Advertisement

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
Advertisement

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
Last updated: January 2026
Advertisement