Practical guide

Learn Regex from Scratch

Regex is a pattern language. Instead of searching only for an exact word, you describe the shape of the text you want to find.

Quick example

To find any digit, use \d. To require three consecutive digits, use \d{3}. To ensure the entire string contains exactly three digits, use ^\d{3}$.

Regex: ^\d{3}$
Matches: 123
Rejects: A123, 12, 1234

1. Literals and dot

Plain text matches plain text. The dot . works as a wildcard for one character. So, c.t matches cat, cot and cut.

2. Character classes

Brackets limit the possibilities. [aei] means one of the listed letters; [A-Z] represents an uppercase letter. With [^0-9], the caret inside the class negates the set.

3. Quantifiers

+ requires one or more occurrences, * allows zero or more, ? makes the previous item optional and {n} requires an exact amount.

4. Special classes

\d represents digits, \w word characters and \s whitespace. Uppercase versions usually represent the opposite.

5. Anchors

^ marks the beginning and $ the end. They are essential when you want to validate the entire string instead of just finding a substring.

6. Groups and alternation

(cat|dog) accepts either alternative. Groups also let you apply quantifiers to entire blocks.

Practice instead of memorizing

The game introduces these concepts progressively and shows exactly which values your expression matched or missed.

Start the levels