Bulk Replace
Reference

Regex Cheat Sheet for Find and Replace: Patterns You Will Actually Use

A practical regex cheat sheet for find and replace: anchors, classes, quantifiers, groups, look-arounds and replacement syntax, with real file examples.

Updated 4 minute read The Bulk Replace team

This regex cheat sheet covers the syntax you need for find and replace: characters, classes, anchors, quantifiers, groups, look-arounds and the replacement codes such as $1. Each section has examples taken from real file edits. The syntax shown works in .NET (PowerShell and most Windows tools), JavaScript (VS Code) and Boost/PCRE (Notepad++), with differences noted where they matter.

Keep it open next to your editor, and test every pattern on a few sample lines before running it across a folder.

Literal characters and escaping

Most characters match themselves. These have special meanings and must be escaped with a backslash to match literally:

. ^ $ * + ? ( ) [ ] { } | \

To matchWrite
A dot, as in example.comexample\.com
A price like $9.99\$9\.99
A function call init()init\(\)
A Windows path C:\DataC:\\Data

In PowerShell, [regex]::Escape('text') escapes a string for you. Our guide to escaping special characters in regex covers the edge cases.

Character classes

PatternMatches
.Any character except a line break (by default)
\d / \DA digit / anything except a digit
\w / \WA word character (letter, digit, underscore) / anything else
\s / \SWhitespace (space, tab, line break) / anything else
[abc]One of a, b or c
[^abc]Any character except a, b or c
[a-z0-9]A lowercase letter or a digit
[\s\S]Any character at all, including line breaks
\t, \r, \nTab, carriage return, line feed

Note that in .NET, \d and \w include Unicode digits and letters from other scripts, which is usually what you want for text but worth knowing for strict data.

Anchors and boundaries

PatternMatches the position
^Start of the text, or of each line in multiline mode
$End of the text, or of each line in multiline mode
\bA word boundary (between a word character and a non-word character)
\BNot a word boundary
\A / \zVery start / very end of the text, regardless of mode

Example: \bcat\b matches cat but not category. Windows line ending note: in .NET, $ in multiline mode matches before \n but not before \r, so on CRLF files use (?=\r?$) or \r?$ where it matters.

Quantifiers

GreedyLazyMeaning
**?Zero or more
++?One or more
???Zero or one (optional)
{3}Exactly 3
{2,}{2,}?2 or more
{2,5}{2,5}?Between 2 and 5

Greedy quantifiers take as much as they can, lazy ones as little as they can. <.*> on <b>hi</b> matches the whole thing, while <.*?> matches just <b>.

Groups and alternation

PatternMeaning
(abc)Capture group, numbered from 1
(?:abc)Group without capturing
(?<name>abc)Named capture group
a|ba or b
\1Back-reference inside the pattern to group 1

Example: \b(\w+) \1\b finds doubled words such as the the. For the full story, see regex replace with capture groups.

Look-arounds

PatternMeaningExample
(?=x)Followed by x\d+(?=px) matches 12 in 12px
(?!x)Not followed by x\bfoo(?!bar)
(?<=x)Preceded by x(?<=\$)\d+ matches 25 in $25
(?<!x)Not preceded by x(?<!https:)//

Look-arounds check text without including it in the match, so the checked text is not replaced. Look-behind with variable-length patterns works in .NET and modern JavaScript, but Boost and PCRE require a fixed length.

Flags (modes)

Inline flagEffect
(?i)Ignore case
(?m)Multiline: ^ and $ match at every line
(?s)Single-line: . also matches line breaks
(?x)Ignore whitespace in the pattern, allowing comments (.NET, PCRE)

Inline flags go at the start of the pattern. Many tools also offer them as checkboxes, such as Notepad++'s ". matches newline" and every editor's Match case.

Replacement syntax

CodeInsertsSupported in
$1, $2Numbered group.NET, PowerShell, JavaScript, VS Code, Notepad++
${1}Numbered group, safe before a digit.NET, PowerShell, Notepad++
\1Numbered groupNotepad++, sed, Python
${name}Named group.NET, PowerShell
$&The whole match.NET, JavaScript, VS Code
$$A literal dollar sign.NET, JavaScript

Differences between Windows tools

Behavior.NET / PowerShellJavaScript / VS CodeBoost / Notepad++
Group in replacement$1$1$1 or \1
Variable-length look-behindYesYes (modern engines)No, fixed length only
Case-insensitive by default-replace yes; [regex] noNo, toggle Match CaseNo, toggle Match case
Any line ending\r?\n\n (VS Code handles CRLF in file search)\R or \r?\n
Change case in replacementNot in plain -replace\U$1, \L$1 in VS Code\U$1, \L$1

When a pattern copied from the web does not work, a flavor difference from this table is a likely reason.

How to read a pattern you did not write

  1. Split it at the top-level | characters: each piece is an alternative.
  2. Number the opening parentheses from the left to see which part is $1, $2 and so on. Skip any that start with (?:, (?=, (?!, (?<= or (?<!, which do not capture.
  3. Read each item with the quantifier after it: \d{4} is "four digits", [^"]* is "any run of characters that are not a quote".
  4. Check the anchors and flags at the ends: ^, $, \b and inline flags such as (?i) change what can match.

Ready-made find and replace patterns

TaskFindReplace
DD/MM/YYYY to YYYY-MM-DD\b(\d{2})/(\d{2})/(\d{4})\b$3-$2-$1
Trailing spaces (multiline)[ \t]+(?=\r?$)(empty)
Blank lines (multiline)^[ \t]*\r?\n(empty)
Doubled words\b(\w+) \1\b$1
Multiple spaces to one {2,}(one space)
Swap "Last, First"(\w+), (\w+)$2 $1
http to https for a domainhttp://(www\.)?example\.comhttps://$1example.com

Five habits that prevent regex mistakes

  1. Prefer specific classes such as [^"]* over .*.
  2. Use \b or look-arounds so short patterns do not match inside longer words.
  3. Escape every literal symbol, especially dots in domains and file names.
  4. Check the match count before replacing.
  5. Keep a backup, because a regex can change thousands of lines in a second.

Running patterns across many files

Every pattern on this page can be used in a Bulk Replace rule. A pattern that is right for one line can still surprise you somewhere in a large folder. Bulk Replace lets you switch any rule to regex with capture groups, back-references, anchors, character classes and look-arounds, then shows a before/after diff of every match before it writes anything, with automatic backups and one-click undo. If a pattern refuses to match, see regex replace not working.

Frequently asked questions

Is regex the same in every tool?

The core syntax is shared, but details differ. Look-behind support, replacement syntax ($1 or \1) and flags vary between .NET, JavaScript, PCRE and Boost.

What does .* mean in regex?

The dot matches any character except a line break and * means zero or more times, so .* matches the rest of the line. Add ? to make it match as little as possible.

How do I match a literal dot or bracket?

Escape it with a backslash: \. matches a dot, \( a parenthesis and \[ a square bracket.

Get three guides like this every month, plus software update notices.Subscribe →