Find and Replace: A Bad Regex Spoils the Batch

Open Tool Find & Replace

Literal and regex matching

Find and replace runs in two modes: literal treats the search text as plain characters, and regex treats it as a pattern where a dot means any character, an asterisk means repetition and brackets mean character sets. The first is safe, the second powerful and easy to overreach. The tool offers case sensitivity, whole-word and regex toggles. The difference matters: literal never eats a paragraph over one full stop, and regex will.

Steps

  1. Paste the text and back it up first.
  2. Enter the search text and the replacement, leaving replacement empty to delete.
  3. Enable case sensitivity for English and whole-word matching when needed.
  4. Turn on regex only when patterns are truly required, trying the sample first.
  5. Replace all, then read through for collateral damage.
  6. Copy or download the result.

Metacharacters, groups, loops

Regex reserves a set of characters, the dot, star, plus, question mark, brackets, braces, parens, pipe, caret and dollar sign, each with meaning, so matching them literally needs a backslash. Parentheses group, and replacements reference captured text by number, whose syntax varies by environment and silently yields empty output when wrong. Greedy matching takes as much as it can and crosses lines it should not, while a trailing question mark makes it lazy. If the replacement contains the search text, repeated execution stacks changes, even though one pass is usually safe. Content containing dollar signs needs checking against reference interpretation.

Common mistakes

Leaving regex on for a literal replacement so a full stop matches anything. Unescaped metacharacters wrecking the batch. Wrong group numbers producing empty output. Greedy matches eating extra lines. Replacements re-matching the search and stacking on repeat runs. And no backup when something breaks.

Where replacement must stop

Structured content needs a parser, since nested HTML and JSON defeat simple patterns. Code changes go through version control with a diff before commit. Config and production data are rehearsed on a copy. Semantic rewriting is beyond regex. And running a wholesale replace somewhere irreversible is never right.

⏫︎