Learn Regex the Easy Way, Part 3: Character Classes
Learn Regex the Easy Way, Part 3: Character Classes
Quick Recap
In Part 2, we learned about anchors and boundaries. The caret ^ matches the start of a line, $ matches the end, and \b matches word edges. They match positions, not characters. Now let’s match actual characters, but with more control than just typing them out literally.
Square Brackets Create a Character Class
A character class is a set of characters inside square brackets: [abc]. It matches any ONE character from that set. Not all of them. Not some of them. Exactly one character from the options inside the brackets.
[abc] Match a single character: a, b, or c
MATCH THESE
abcDO NOT MATCH THESE
deababc
Notice that [abc] does NOT match “ab” or “abc.” It matches exactly one character. If your text contains “ab,” the regex will match just the “a” (the first character from the set it finds).
Ranges
You...