Character Classes in Regular Expressions
1. Character Classes Overview
Character classes in regular expressions allow you to specify a set of characters that can match a single character position. They are enclosed in square brackets []
and provide a concise way to define patterns that match any one of a group of characters.
2. Basic Character Classes
Basic character classes match any one character from a specified set. For example:
[abc]
- Matches any one of the characters 'a', 'b', or 'c'.[0-9]
- Matches any digit from 0 to 9.[A-Z]
- Matches any uppercase letter from A to Z.[a-z]
- Matches any lowercase letter from a to z.
Example: The pattern [aeiou]
matches any single vowel:
Text: "The quick brown fox jumps over the lazy dog."
Matches: "e", "u", "i", "o", "o", "u", "o", "e", "e", "a", "o"
3. Negated Character Classes
Negated character classes match any character that is not in the specified set. They are defined by placing a caret ^
at the beginning of the character class. For example:
[^0-9]
- Matches any character that is not a digit.[^aeiou]
- Matches any character that is not a vowel.
Example: The pattern [^aeiou]
matches any single consonant:
Text: "The quick brown fox jumps over the lazy dog."
Matches: "T", "h", "q", "c", "k", "b", "r", "w", "n", "f", "x", "j", "m", "p", "s", "v", "r", "t", "h", "l", "z", "y", "d", "g"
4. Combining Character Classes
You can combine multiple character classes within a single set of square brackets to create more complex patterns. For example:
[a-zA-Z]
- Matches any letter, either uppercase or lowercase.[0-9a-fA-F]
- Matches any hexadecimal digit (0-9, a-f, A-F).
Example: The pattern [a-zA-Z0-9]
matches any alphanumeric character:
Text: "Hello123World!"
Matches: "H", "e", "l", "l", "o", "1", "2", "3", "W", "o", "r", "l", "d"