Learn Regular Expressions
Find, validate and rewrite text with compact search patterns.
Free to start. Built from a single prompt. No credit card.
Regular expressions, or regex, are a compact language for describing text patterns, such as "three digits, a dash, then four digits" or "a line that starts with ERROR". They power search and replace in code editors, input validation, log analysis and tools like grep and sed, and nearly every programming language supports them. The syntax looks cryptic at first, but it is built from a small set of pieces: literals, character classes, quantifiers, anchors and groups. Learnr builds a personalized regex course from your prompt and teaches the patterns through short, interactive lessons.
How to learn Regular Expressions, step by step
- Use an interactive tester. Open a tester such as regex101.com, pick the flavor matching your language, and paste in sample text. Its live highlighting and step-by-step explanation show exactly which part of the pattern matched which characters.
- Learn the metacharacters and how to escape them. Know that . matches any character, and that characters like ., *, +, ?, (, ), [ and \ have special meaning. Escape them with a backslash when you mean them literally, as in \. to match a real period.
- Build patterns from classes and quantifiers. Combine classes like [A-Za-z] and \d with quantifiers such as + and {3}. Write a pattern for a US ZIP code, a hex color like #1a2b3c and a date like 2024-03-15, testing matches and near misses each time.
- Understand greedy versus lazy matching. By default, .* grabs as much as possible, so <.*> on HTML swallows several tags at once. Add ? to make it lazy, or better, use a negated class like <[^>]*> that cannot run past the closing bracket.
- Capture and replace with groups. Wrap parts in parentheses to capture them, then reorder them in replacements, for example turning 2024-03-15 into 15/03/2024. Use non-capturing groups (?:...) for alternation you do not need to keep, and named groups for readability.
- Practice on real text every day. Use regex in your editor's find bar, in grep -E on log files and in your language's regex library. Small daily Learnr exercises help the symbols stick so you stop reaching for a cheat sheet.
What you'll learn
- Match characters with classes like [a-z], \d, \w and \s
- Control repetition with *, +, ? and {n,m}, greedy or lazy
- Anchor patterns with ^, $ and word boundaries
- Capture groups and reuse them in search and replace
- Use lookahead and lookbehind and avoid catastrophic backtracking
What's inside your Regular Expressions course
A sample path Learnr builds for Regular Expressions. Your real course adapts to your goal and level.
- 1. Literals, metacharacters and escaping
- 2. Character classes and shorthand classes
- 3. Quantifiers, greedy and lazy matching
- 4. Anchors, boundaries and flags
- 5. Groups, alternation, backreferences and replacement
- 6. Lookarounds, flavors and performance
How long does it take to learn Regular Expressions?
The core syntax of classes, quantifiers, anchors and groups takes about 1 to 2 weeks of short daily practice. Reading complex patterns quickly and using lookarounds confidently develops over a few months of using regex in real tasks.
What you need before you start
Comfort working with text in a code editor or terminal. Some programming experience helps you apply regex in code, but you can learn the syntax with just an online tester.
Common mistakes to avoid
- Forgetting that regex flavors differ. JavaScript, Python, PCRE and POSIX grep support different features, so set your tester to the flavor you will actually use.
- Writing nested quantifiers like (a+)+ that cause catastrophic backtracking on certain inputs. Keep patterns specific and avoid ambiguous repetition.
- Trying to parse HTML or JSON with regex. Use a proper parser for nested formats, and save regex for flat patterns in text.
How Learnr builds your course
Type your topic
Tell Learnr you want to learn Regular Expressions, or get as specific as you like.
Get a personalized course
Learnr instantly builds a full Regular Expressions course tailored to your goal and skill level.
Learn and level up
Move through bite-sized lessons and interactive practice, earning XP and keeping your streak.
Regular Expressions questions, answered
Should I use regex to validate email addresses?
A simple pattern that checks for something, an @ and a domain with a dot catches typos. Fully matching the email specification with regex is impractical, so the reliable check is sending a confirmation message.
Are regular expressions the same in every language?
The basics are shared, but flavors differ in details such as lookbehind support, named group syntax and flags. Learn the core once, then check the documentation for your language or tool.
Explore more courses
More programming and tech courses
- Learn PowerShell
- Learn Excel VBA
- Learn WordPress
- Learn Object-Oriented Programming
- Learn Functional Programming
- Learn Design Patterns
- Learn Software Testing
- Learn Computer Architecture
See all programming and tech courses
Ready to learn Regular Expressions?
Start your free, personalized Regular Expressions course in seconds. .