A practical guide to regex find and replace
Move beyond literal search with patterns, capture groups, and case control, plus the pitfalls that trip people up when editing text.
Literal search versus patterns
In literal mode the find field is escaped, which means every character is matched exactly as typed. That is what you want when you replace a real word or a fixed phrase. Regex mode instead reads the field as a pattern language, so symbols like the dot, plus sign, and backslash take on special meaning. Switching to regex unlocks matches that literal text cannot express, such as any three digits or any run of spaces, at the cost of learning a small syntax.
The building blocks you will use most
A handful of tokens cover the majority of real edits. The class \d matches a digit and \w matches a letter, digit, or underscore, and adding a plus sign after either matches one or more of them. The \s token matches whitespace, and \b marks a word boundary so \bcat\b will not touch the word category. Square brackets build custom sets like [aeiou], and the pipe inside parentheses offers alternatives such as (cat|dog).
Capture groups and back references
Wrapping part of a pattern in parentheses captures whatever it matched, numbered from left to right. In the replacement field you recall those captures with $1, $2, and so on, which lets you rearrange rather than merely overwrite text. Turning First Last into Last, First is a classic example: find (\w+) (\w+) and replace with $2, $1. Because the replacement runs through the same engine, a stray dollar sign in ordinary text should be written carefully to avoid an unintended reference.
Common mistakes and how to avoid them
The most frequent surprise is forgetting that regex mode gives special meaning to punctuation, so searching for a literal period as a pattern matches every character instead. Escape it as a backslash then dot, or simply switch off regex. Greedy quantifiers are another trap, since a pattern like .+ will stretch as far as it can across a line. When a replacement seems to do too much, check the replacement count and untick 'Replace all' to step through matches one at a time.