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 match | Write |
|---|---|
| A dot, as in example.com | example\.com |
| A price like $9.99 | \$9\.99 |
| A function call init() | init\(\) |
| A Windows path C:\Data | C:\\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
| Pattern | Matches |
|---|---|
. | Any character except a line break (by default) |
\d / \D | A digit / anything except a digit |
\w / \W | A word character (letter, digit, underscore) / anything else |
\s / \S | Whitespace (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, \n | Tab, 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
| Pattern | Matches the position |
|---|---|
^ | Start of the text, or of each line in multiline mode |
$ | End of the text, or of each line in multiline mode |
\b | A word boundary (between a word character and a non-word character) |
\B | Not a word boundary |
\A / \z | Very 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
| Greedy | Lazy | Meaning |
|---|---|---|
* | *? | 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
| Pattern | Meaning |
|---|---|
(abc) | Capture group, numbered from 1 |
(?:abc) | Group without capturing |
(?<name>abc) | Named capture group |
a|b | a or b |
\1 | Back-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
| Pattern | Meaning | Example |
|---|---|---|
(?=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 flag | Effect |
|---|---|
(?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
| Code | Inserts | Supported in |
|---|---|---|
$1, $2 | Numbered group | .NET, PowerShell, JavaScript, VS Code, Notepad++ |
${1} | Numbered group, safe before a digit | .NET, PowerShell, Notepad++ |
\1 | Numbered group | Notepad++, 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 / PowerShell | JavaScript / VS Code | Boost / Notepad++ |
|---|---|---|---|
| Group in replacement | $1 | $1 | $1 or \1 |
| Variable-length look-behind | Yes | Yes (modern engines) | No, fixed length only |
| Case-insensitive by default | -replace yes; [regex] no | No, toggle Match Case | No, toggle Match case |
| Any line ending | \r?\n | \n (VS Code handles CRLF in file search) | \R or \r?\n |
| Change case in replacement | Not 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
- Split it at the top-level
|characters: each piece is an alternative. - Number the opening parentheses from the left to see which part is
$1,$2and so on. Skip any that start with(?:,(?=,(?!,(?<=or(?<!, which do not capture. - Read each item with the quantifier after it:
\d{4}is "four digits",[^"]*is "any run of characters that are not a quote". - Check the anchors and flags at the ends:
^,$,\band inline flags such as(?i)change what can match.
Ready-made find and replace patterns
| Task | Find | Replace |
|---|---|---|
| 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 domain | http://(www\.)?example\.com | https://$1example.com |
Five habits that prevent regex mistakes
- Prefer specific classes such as
[^"]*over.*. - Use
\bor look-arounds so short patterns do not match inside longer words. - Escape every literal symbol, especially dots in domains and file names.
- Check the match count before replacing.
- 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.
