MEPX
Chapter 2 of 10All chapters

Chapter 2 of 10

Literals and classes

Matching characters.

Character classes

Square brackets match any one character from a set. Ranges work inside them, and a leading caret negates the whole set.

  • [aeiou] is one vowel, [0-9] is one digit, [^0-9] is one non-digit.
  • Shorthands: \d digit, \w word character, \s whitespace, and their capitals negate.

The dot

A dot matches any character except a newline by default. It is the most overused piece of syntax, and replacing it with a narrower class usually makes a pattern both faster and clearer.