Character Classes

Match specific character sets

#character-classes #basics #patterns

Character Classes

Character classes match specific sets of characters.

Basic Classes

\d    Match any digit (0-9)
\D    Match any non-digit
\w    Match word character (a-z, A-Z, 0-9, _)
\W    Match non-word character
\s    Match whitespace (space, tab, newline)
\S    Match non-whitespace
.     Match any character except newline

Custom Character Sets

[abc]      Match a, b, or c
[^abc]     Match any character except a, b, or c
[a-z]      Match any lowercase letter
[A-Z]      Match any uppercase letter
[0-9]      Match any digit
[a-zA-Z]   Match any letter

Examples

# Match email pattern
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

# Match phone number
\d{3}-\d{3}-\d{4}

# Match hex color
#[0-9A-Fa-f]{6}

Discover another handy tool from EditPDF.pro