Regex in find and replace: a gentle guide with seven recipes
A regular expression, or regex, is a pattern written in a few symbols. In the find-and-replace tool, switch on Regex and the Find box reads a pattern instead of plain text, so a full stop means any character rather than a full stop. Most useful patterns need only a handful of symbols. Turn Regex off again for ordinary words, because then every symbol is literal.
Find and replaceReplace a word everywhereOpenThe symbols you need
| Symbol | Matches |
|---|---|
| . | any one character except a line break |
| * | the item before it, zero or more times |
| + | the item before it, one or more times |
| ? | the item before it, once or not at all |
| [ ] | one character from the list, such as [aeiou] or [0-9] |
| \d \w \s | a digit; a letter, digit or underscore; a space, tab or line break |
| ( ) | a group, which Replace can reuse as $1, $2 and so on |
| ^ and $ | the start and the end of the whole text |
Capital letters reverse a class: \D is any non-digit and \S any non-space.
Two notes apply to this tool. First, \w matches only English letters, digits and the underscore. For accented letters, use \p{L}, which matches a letter in any alphabet. Second, ^ and $ match the start and end of the whole text, not of each line. The recipes below use a line break inside the pattern instead.
Groups and $1
Put part of a pattern in parentheses and the tool remembers what that part matched. In Replace, $1 is the first group, $2 the second, and $& is the whole match. The tool's own FAQ gives a classic example: with Find set to (\w+)@(\w+) and Replace set to $2 at $1, the text name@site becomes site at name. Groups work only with Regex on. With it off, $1 is typed as it is.
Build a pattern in steps
Start with the exact text you want to find, then replace the parts that vary with symbols. A date such as 2026-11-04 can be written as \d{4}-\d{2}-\d{2}, where {4} means exactly four of the item before it. Test the simple version first, and tighten it only when the highlights show it catches too much.
Seven recipes
Turn Regex on for each recipe. The tool highlights the matches and shows their count before you copy, so test each recipe on a copy of your text first.
- Swap first and last name. Find
(\w+) (\w+), Replace$2, $1. "Ada Lovelace" becomes "Lovelace, Ada". For names with accents, use(\p{L}+) (\p{L}+). This handles two-word names only. - Remove trailing spaces. Find
[ \t]+(?=\r?\n|$), Replace left empty. It matches spaces or tabs that come just before a line break or the end of the text. The lookahead(?=...)checks for the break without removing it, and\r?covers Windows line endings. Fix pasted text has a Trim each line option that does the same job. - Collapse double spaces. Find
{2,}, which is a space repeated two or more times, and Replace one space. Two or more spaces become one. It also shrinks indentation, so skip it for code. - Add a prefix to every line. Find
(.+), Replace- $1. Each run of one or more characters between line breaks is a line, and $1 holds it. Blank lines are skipped. Use the one-or-more form,.+, and not the zero-or-more form, because the zero-or-more form also matches the empty spots between line breaks, so blank lines gain a stray dash. - Delete blank lines. Find
\n\s*(?=\n), Replace left empty. The match is a line break and any whitespace after it, up to the next line break, and that next break is kept. This removes blank lines, including ones that hold only spaces. Because\salso matches carriage returns, Windows line endings work too. A blank line at the very top of the text stays. - One item per line. Find
,[ ]*, Replace\n, and tick Read \n and \t in Replace. "apples, pears, plums" becomes three lines. Without the tick, \n stays as the two characters backslash and n. - Wrap each number in brackets. Find
\d+, Replace[$&]. "Order 42 and 7" becomes "Order [42] and [7]". $& is the whole match, so no group is needed.
Check before you copy
- The match count and highlights show what the pattern caught. If the highlights are wrong, the result will be too.
- A pattern with a mistake shows "Not a valid pattern" under the boxes. Fix the pattern and the result updates.
- Leave Replace empty to delete every match. The box's placeholder says so.
- Match case is off by default, so recipes match upper and lower case alike. Turn it on when case matters.
- To find a literal full stop or bracket with Regex on, put a backslash in front: \. or \[.
Edge cases
- Greedy matches.
.+takes as much as it can. To stop at the first match, add a question mark:.+?. This matters for text like<b>.+</b>, which can run across several tags. - Whole words. For a plain word, Regex off is safer. Whole word only stops cat matching inside category, as the tool's FAQ explains.
- Keep the original. The result appears in its own box, and the input is left as it was. Copy the result out when you are satisfied.
