Frequently Asked Question

What do regex anchors and character classes actually do?

An anchor matches a position in the string rather than a character. ^ matches the start of a line and $ matches the end, so ^Error finds lines that begin with the word Error and done$ finds lines that end with done. \b matches a word boundary, useful when you want \bcat\b to match cat but not cattle or concatenate. Anchors are how you turn a substring match into a structural match.

A character class matches any one character from a set. [aeiou] matches any vowel; [0-9] matches any digit; [A-Za-z] matches any ASCII letter. A leading ^ inside the brackets negates the class, so [^0-9] matches any non-digit. POSIX provides named classes like [[:digit:]], [[:alpha:]], and [[:space:]] that respect the current locale, and PCRE adds shorthand \d, \w, \s (and their negated forms \D, \W, \S). Combined with quantifiers (*, +, ?, {n,m}), anchors and character classes cover most realistic patterns.

Video

Further reading and video