Developer
Regex Tutorial: Patterns for Real-World Matching
Regular expressions — regex — are the swiss army knife of text processing: validating input, extracting parts of strings, finding patterns in logs, and powering search-and-replace across entire codebases. They are also famously cryptic. This tutorial builds the concepts from the ground up so you can read and write practical patterns instead of copying them blindly.
What a regex is
A regex is a pattern that describes a set of strings. The regex engine walks through your text and finds every place where the pattern matches. Everything in regex is about describing what characters appear and how many times.
Literals match themselves
The simplest pattern is a literal string. The pattern cat matches the letters c-a-t anywhere in the text. That is useful for searching but not very expressive — the real power comes from the special syntax.
Character classes
Square brackets match one character from a set:
[abc]— one of a, b, or c[a-z]— any lowercase letter[0-9]— any digit (same asd)[^0-9]— anything except a digit (the caret negates)
Shortcuts cover the common sets: d for digits, w for word characters (letters, digits, underscore), s for whitespace. The uppercase versions — D, W, S — match the opposite.
Quantifiers: how many
Quantifiers say how many times the preceding element may repeat:
*— zero or more+— one or more?— zero or one (optional){3}— exactly three{2,5}— between two and five
So d{4} matches exactly four digits — handy for years or ZIP codes. A practical email-ish pattern: [w.+-]+@[w-]+.[w.]+.
Anchors: where in the text
Anchors do not match characters; they match positions. ^ matches the start of the string and $ matches the end. matches a word boundary. Using anchors is what turns “find a digit anywhere” into “the whole string is a digit” — critical for validation: ^d{10}$ matches a string of exactly ten digits and nothing else.
Groups and alternation
Parentheses group parts of a pattern so quantifiers apply to the whole group, and capture the matched text for later use:
(ab)+— one or more repetitions of “ab”(d{2})/(d{2})/(d{4})— captures a date's day, month, and year separatelycat|dog— alternation: match “cat” or “dog”
Non-capturing groups (?:...) group without capturing — useful when you need the grouping but not the captured value.
Escaping: matching special characters
To match a character that has special meaning — a literal dot, plus, or parenthesis — escape it with a backslash: . matches a period, + matches a plus. This is the most common beginner mistake: forgetting the backslash and wondering why a.c matches “abc” instead of “a.c”.
Common mistakes
- Using
.*too greedily — it matches as much as possible; add a?(.*?) for lazy matching - Forgetting to escape dots and other metacharacters
- Testing only against matches, not non-matches — a validation pattern must reject bad input too
- Copying a pattern from the internet without understanding what it allows
Build patterns from plain English
When you know what you want but are unsure of the syntax, Regex Generator converts a plain-English description into a starting pattern you can then test and refine. Pair it with a real corpus of examples and counter-examples, and verify both sides before relying on the pattern in code.
Related developer tools
JSON Formatter
Format, validate, and inspect JSON instantly. Essential for debugging API responses and configuration files.
Regex Generator
Generate a ready-to-use regex for common cases such as email or URL matching.
SQL Formatter
Format and beautify raw SQL queries for better readability and debugging.
JWT Decoder
Decode JSON Web Tokens and inspect the header and payload contents.
YAML Validator
Run a basic indentation check on your YAML and flag obvious spacing issues.
Markdown Preview
Preview how your Markdown content will render as HTML with live conversion.
More developer guides
JSON Formatting and Validation: A Developer's Guide to Clean Data
Master JSON formatting, validation, and debugging with practical examples and best practices.
JWT Explained: What a JSON Web Token Actually Is
Header, payload, signature — and why decoding a JWT is not the same as verifying it. A security-accurate explainer.
SQL Formatting Best Practices: Readable Queries That Scale
Consistent indentation, capitalized keywords, clear aliases — the formatting habits that make SQL maintainable.
YAML Guide: Syntax, Common Errors, and When to Use It
Indentation, mappings, arrays, and the YAML gotchas that break config files — plus how YAML compares to JSON.
Markdown Cheat Sheet: Everything You Need to Write Better Docs
Headers, lists, tables, code blocks, and links — the only Markdown guide you will need for READMEs and docs.
Why Browser-Based Tools Are the Safest Way to Handle Sensitive Documents
Discover why processing files in your browser protects privacy better than desktop or cloud alternatives.
Was this guide helpful?
Browse more tools and guides to get your work done faster — all in your browser, no account needed.
Explore all tools