Regex Character Sets

Working with character classes

Basic Sets

[abc] # matches a, b, or c
[^abc] # matches anything except a, b, c
[a-z] # matches a through z
[A-Z] # matches A through Z
[0-9] # matches 0 through 9

Combined Ranges

[a-zA-Z] # any letter
[a-zA-Z0-9] # any alphanumeric
[a-z0-9_] # lowercase, digits, underscore

Special in Sets

[.] # literal dot in set
[*] # literal asterisk in set
[\-] # literal hyphen
[\]] # literal closing bracket

Examples

[aeiou] # vowels
[^aeiou] # consonants
[0-9a-fA-F] # hexadecimal digit