Regular Expressions tutorial
Learn how to find, check, and replace text with regular expressions.
Course contents
Foundations
- Introduction to Regex — What a regular expression is, how the engine searches text, the everyday jobs regex does, the key words you will meet, and why flavors differ.
- Literal Matching — How each character in a pattern matches itself, why order and case matter, the i flag, and how to read match results, positions and groups.
- Pattern Escaping — How the backslash turns special regex characters into plain ones, which characters are reserved, escape sequences, and matching file and Windows paths.
Character Sets & Boundaries
- Custom Character Classes — How square brackets let one spot in a pattern match any character from a set: character sets, ranges like a-z, negated classes, and escaping inside classes.
- Shorthand Classes — The short codes \d, \w and \s and their opposites, the dot and the s flag, and how to match accented letters and other Unicode characters with \p{L} and the u flag.
- Anchors — How anchors match positions instead of characters: the start and end anchors, multiline mode and the m flag, word boundaries, and checking a whole value.
Repetition & Quantifiers
- Quantifiers — How quantifiers repeat part of a pattern: zero or more, one or more, optional, exact counts, range counts and open ranges.
- Greedy Matching — Why quantifiers are greedy, how lazy quantifiers and negated classes stop over-matching, and how nested quantifiers and near matches lead to ReDoS.
Structure and Capture
- Alternation — How the | operator offers choices, why it splits the whole pattern, alternative groups, match priority, classes versus alternation, and common prefixes.
- Capture Groups — How round brackets capture parts of a match: numbered groups, how groups are counted, nested groups, and non-capturing groups.
- Backreferences — How to name groups, match the same text again with backreferences, refer to groups by number or name, and find repeated words and patterns.
Advanced Assertions
- Lookarounds — How lookaheads and lookbehinds check the text around a match without including it: password rules, conditional matching, context and prefix checks.
Working with Text and Logs
- Text Modification — How to pull fields, CSV values, quoted strings and marked text out of text, and how to change text with static, group, dynamic and zero-width replacements.
- Regex Validation — The rules of a good validator, plus practical patterns for emails, URLs, phone numbers, postal codes, IP addresses, UUIDs, hashes, hex colors and versions.
- Log Processing — How to parse log lines into fields, filter errors, use lookarounds on logs, convert dates to ISO 8601, find duplicates, clean spacing and mask private data.
Getting It Right
- Regex Debugging — How to fix a pattern that matches nothing, too much, too little or the wrong text, how to isolate the broken piece, verify a pattern and read error messages.
- Regex Performance — How backtracking and match paths work, why nested quantifiers explode, what catastrophic backtracking and ReDoS are, and how to write fail-fast patterns.
Reference
- Cheat Sheet — Every pattern from the course in one place: anchors, classes, quantifiers, groups, lookarounds, flags, validators, log parsing, extraction and formatting.
- Regex Troubleshooting — Fixes for patterns that match too much, too little or the wrong text, flag mistakes, error messages, Unicode problems, slow patterns, and a step-by-step method.