Text & Writing
Regex Cheatsheet for Everyday Text Work
A practical regex cheatsheet for writers and editors: tokens, anchors, quantifiers, and copy-paste recipes you can test safely.
Overview
You do not need to be a compiler engineer to use regular expressions for text work. Most editing tasks need a short vocabulary: match a digit, a word character, optional whitespace, start or end of a line, and a repeated pattern. This cheatsheet focuses on those everyday moves.
Treat every pattern as a hypothesis. Paste sample paragraphs into a tester, confirm what matched, and only then run a replace across a whole document. Regex is powerful enough to delete half an article with one over-broad replace—test first.
Use Dockzio’s Regex Tester for live experiments. When you are cleaning length or scanning vocabulary after a replace, the Word Counter helps you see how much the text changed. For deeper engine concepts, see the Developer article on regex basics.
Step-by-step
- 1. Core tokens at a glance
`\d` digit, `\w` word character (letters/digits/underscore in many flavors), `\s` whitespace, `.` any character except newline (flavor-dependent). Capitalize the letter for negation: `\D`, `\W`, `\S`.
Character classes like `[A-Za-z]` and `[^0-9]` pin down allowed or forbidden sets. Escape special characters when you mean them literally: `\.`, `\(`, `\)`.
- 2. Quantifiers and optionality
`?` optional (0–1), `*` zero or more, `+` one or more, `{n}` exactly n, `{n,m}` between n and m. Prefer explicit counts for IDs and codes (`\d{5}`) over open-ended `.*`.
Lazy forms (`*?`, `+?`) help when you want the shortest match between two delimiters, such as text inside quotes.
- 3. Anchors and line-aware editing
`^` start of string (or line in multiline mode), `$` end of string/line, `\b` word boundary. Anchors stop you from matching a code that only appears inside a longer token.
When cleaning lists, multiline mode plus `^\s*-\s*` style patterns can target bullet prefixes without touching mid-sentence hyphens—verify on a sample first.
- 4. Everyday recipes to adapt
Extra spaces: `{2,}` on spaces, or `\s+` when collapsing general whitespace (careful with newlines). Trailing spaces: `\s+$` per line with multiline on. Simple emails (pragmatic, not perfect): `\b\S+@\S+\.\S+\b`.
Repeated blank lines, date-like digits, and markdown heading markers are good practice targets. Keep a personal note of patterns you reuse.
- 5. Groups and safe replace workflow
Parentheses capture; use `$1` / `\1` style backreferences in replacements depending on the tool. Non-capturing `(?:...)` groups structure alternation without cluttering replace slots.
Workflow: (1) match only, (2) review highlights, (3) replace on a copy, (4) skim word count and meaning. Never invent a replace pattern you have not matched first.
- 6. Know when not to use regex
Nested markup, full HTML, and natural-language grammar are poor regex targets. Use a markdown-aware or HTML-aware tool for structure; keep regex for flat text patterns.
If a pattern takes more than a few minutes to get right, a dedicated formatter or three-step manual edit may be faster and safer.
Common mistakes
- Greedy `.*` across the whole document. A single greedy match can swallow from the first quote to the last. Prefer tighter classes or lazy quantifiers, and test on multi-occurrence samples.
- Forgetting to escape dots in filenames and URLs. `file.txt` as a pattern matches `fileXtxt`. Use `file\.txt` when you mean a literal dot.
- Running replace-all without a dry run. Always highlight matches first. Replace-all is irreversible in many editors unless you undo immediately.
- Copying a pattern from another regex flavor blindly. Lookarounds and Unicode escapes differ. Re-test in the Regex Tester (JavaScript-style) if that matches your environment.
FAQ
Quick answers to common questions.
Related Dockzio tools
Practice the concepts from this guide with free browser tools — files stay on your device.
- Regex TesterDeveloperTest JavaScript regular expressions with matches and groups.
- Word CounterTextCount words, characters, sentences, and reading time.
Browse categories:Text & Writing →More in Text & Writing →
Suggested next reading
- Regex Basics: Patterns You Can Trust7 min · A practical introduction to regular expressions—literals, character classes, quantifiers, groups, and safe testing habits.
- Cleaning Text Lists: Dedupe, Sort, and Normalize5 min · A practical workflow to deduplicate, sort, and normalize messy lists for tags, inventories, and imports.
- Word Count Tips for Clear, On-Brief Writing5 min · Practical ways to hit word and character limits, trim fluff, and check structure without losing meaning.
- Case Conversion Without Breaking Meaning5 min · How to switch title case, sentence case, snake_case, and camelCase safely—plus cleanup steps for messy lists.
Newsletter
Production intelligence in your inbox
Get practical guides on PDF/X, color, press profiles, and production workflows — written for commercial print teams.