Character classes are a set of characters that we group together to find a match with any of the characters in the group.
They are defined using square brackets [], and inside them we specify the characters we want to search for.
For example, the expression [abc] will match any of the characters a, b, or c. This means that any string containing at least one of these characters will satisfy the pattern.
Simple Character Classes
We can create simple character classes simply by putting several individual characters into the group. For example:
[abc]: Matches any of the charactersa,b, orc[aeiou]: Matches any vowel
Let’s see an example.
By using [ao], we are finding all the letters a or o.
Character Ranges
We can also use a range inside the character class to cover a set of contiguous characters.
To do this, we use the separator -. For example:
[a-z]: Matches any lowercase letter[A-Z]: Matches any uppercase letter[A-Za-z]: Matches any letter, whether uppercase or lowercase[0-9]: Matches any digit
Let’s see an example.
Here the pattern [a-f] searches for all letters from a to f.
Negated Character Classes
We can also negate a character class by using the ^ symbol at the beginning of the class.
In this case, the characters that will satisfy the pattern are those not within the range. For example:
[^abc]: Matches any character that is nota,b, orc.[^a-zA-Z]: Matches any character that is not a letter.[^0-9]: Matches any character that is not a digit.
In this case, [^a-zA-Z] searches for everything that is not a letter. That is, it finds numbers, the period, spaces.
