How to Learn Regular Expressions from Scratch with Interactive Exercises
Updated
Start with RegexOne's Lesson 1 and type abc into the exercise field to match the first three lines. That single action captures the core idea: a regular expression is a pattern of characters you write to match a specific sequence in text. This approach works if you learn best by doing short exercises and getting immediate feedback; it assumes no prior regex knowledge and only requires a browser.
What a regular expression actually is
A regular expression (also written regex or regexp) is a pattern you use to find, extract, or validate text such as code, log files, spreadsheets, or documents. RegexOne frames the first principle plainly: everything is essentially a character, and you are writing patterns to match a specific sequence of characters (a string).
Most patterns use normal ASCII — letters, digits, punctuation, and symbols like %#$@! from your keyboard — but Unicode characters can also be used to match international text.
How the interactive lessons work
Each lesson introduces one new syntax or concept, then gives you lines of text to match. As you type into the input field, matching characters are highlighted on each line. To move to the next lesson, your pattern must match all the lines provided. This forces you to generalize rather than hard-code a single answer.
In Exercise 1, the task is to match three lines:
match abcdefg
match abcde
match abc
Typing abc matches all three, because those are the common characters on each line. Solve the task to continue to the next problem.
The lesson sequence
RegexOne orders lessons so each builds on the last. Following this order avoids the common trap of jumping to advanced syntax before the basics are automatic:
| Lesson | Topic |
|---|---|
| 1 | Introduction and the ABCs (matching characters) |
| 1½ | The 123s (digits) |
| 2 | The dot |
| 3 | Matching specific characters |
| 4 | Excluding specific characters |
| 5 | Character ranges |
| 6 | Catching some zzz's (repetition) |
| 7 | Mr. Kleene (the Kleene star) |
| 8 | Characters optional |
| 9 | All this whitespace |
| 10 | Starting and ending (anchors) |
| 11 | Match groups |
| 12 | Nested groups |
| 13 | More group work |
| 14 | It's all conditional |
| 15 | Other special characters |
Practice with real problems
After the lessons, RegexOne provides practice problems that apply patterns to realistic tasks:
- Matching decimal numbers
- Matching phone numbers
- Matching emails
- Matching HTML
- Matching specific filenames
- Trimming whitespace from the start and end of a line
- Extracting information from a log file
- Parsing and extracting data from a URL
These are the patterns you will actually reuse, so treat them as the test of whether the earlier lessons stuck.
Language-specific guides
Regex syntax varies slightly between languages. RegexOne offers guides for C#, JavaScript, Java, PHP, and Python, so you can check how a pattern you learned applies in the language you write.
A practical way to work through it
- Do one lesson at a time and type patterns rather than reading solutions.
- When a pattern fails, change one element at a time to see which character breaks the match.
- After Lesson 10, attempt the practice problems before moving to groups and conditionals.
- Revisit any lesson where you had to guess rather than reason.
The site lists PayPal among its payment platforms, but the lesson content described here is presented as an interactive tutorial; check the site directly for what is free versus paid.