CHARACTER CLASSES

Character sets

Square brackets: match one character out of a set.

A character set [ae] matches exactly one character that is either a or e. gr[ae]y matches gray and grey, nothing else. Brackets mean "pick one of these". Inside them, listing order does not matter and duplicates are ignored.

A set always consumes exactly one character, like the dot - just restricted to your list. [abc][abc] needs two characters, so it matches "ab" or "ca" but not the single "a". Listing long runs out by hand gets old fast, which is what ranges fix.

Most metacharacters lose their power inside brackets: [.] is a literal dot. The characters that stay special inside a set are ] ^ - and the backslash.

EXERCISE

Match both spellings: gray and grey.

/ /
gray hair
must match: "gray"
grey skies over Warsaw
must match: "grey"
groy is not a word
must match nothing