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.
What to read after the complete guide
This guide teaches Regex concepts in sequence. The practice roadmap organizes how to train, while the exercise collection lets you solve individual tasks.
- Five-level Regex practice roadmap — plan positive and negative tests
- Solve the 30 exercises — graded tasks and solutions
- Read the beginner introduction — a shorter first lesson
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, 12341. 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 levelsBuild a pattern from a requirement
Suppose a product code must start with REF-, contain exactly three digits and have nothing extra. Start with the literal REF-, add the digit class with an exact quantifier, then put anchors around the whole pattern. This prevents a code embedded inside an unrelated sentence from being accepted.
Run this example
^REF-\d{3}$Expected match: REF-204
Counterexample: REF-20
Where the rule stops: The Regex checks the structure only; a valid-looking product code may still be missing from your database. Test that separately.
Your turn
Next, replace REF with another prefix and predict which samples should stop matching.
Compare your first pattern with a revised one. Read the test result as evidence, not as a guarantee that every real-world input is covered.
When a Regex matches the wrong thing
Practice with eight documented mistakes and four realistic developer workflows. Compare the broken pattern, the fix, and negative test cases before you reuse a solution.
Open the debugging lab