Bulk Replace
Troubleshooting

Regex Find and Replace Not Working? Common Causes and Fixes

Regex matches nothing, too much, or $1 shows up literally? Fix regex mode, escaping, greedy quantifiers, anchors, line endings and replacement syntax.

Updated 5 minute read The Bulk Replace team

When a regex find and replace is not working, the cause is almost always one of a short list: regex mode is switched off, a special character is not escaped, the pattern is greedy and matches too much, the anchors ^ and $ are not in multiline mode, Windows line endings get in the way, or the replacement uses the wrong syntax for groups ($1 versus \1). Work through the symptoms below in order and you will usually find the problem in a minute or two.

Before you start, copy one line from a real file that should match into a scratch file. Testing against real text, with its real spaces and line endings, finds problems that a retyped example hides.

Symptom 1: the pattern matches nothing

Regex mode is off

Most tools default to plain text. With regex off, \d+ searches for a backslash, a d and a plus sign. In VS Code, click the .* button (Alt+R). In Notepad++, choose Regular expression under Search Mode. In PowerShell, -replace always uses regex, but the .Replace() method never does.

A special character is not escaped

Characters such as . ( ) [ ] ? + * $ ^ | and the backslash have special meanings. A search for init() in regex mode looks for "init" followed by an empty group, which matches init but not the parentheses, so a replacement can produce odd results. Escape them: init\(\). A Windows path like C:\Data must be written C:\\Data. Our guide to escaping special characters in regex has the full list.

Match case is on (or off) when you did not expect it

PowerShell -replace ignores case, while VS Code and Notepad++ use whatever the Match case toggle says. A pattern for Error will not find ERROR with match case on.

The text is not what you think

Two words that look separated by a space may be separated by a tab or a non-breaking space. Quotes may be curly in some files and straight in others. Try a looser pattern such as word1\s+word2, or word1.word2 to find out what is really between them. Hidden characters and mixed encodings are a common cause when a plain search fails too.

The pattern spans lines but the dot cannot

By default, . does not match a line break. <div>.*</div> fails when the closing tag is on another line. Use [\s\S]*? or turn on single-line mode with (?s) (or Notepad++'s . matches newline box).

Symptom 2: the pattern matches too much

Greedy quantifiers

".*" on the line a="1" b="2" matches "1" b="2", from the first quote to the last. Make it lazy with ".*?", or better, use a negated class: "[^"]*". See greedy vs lazy regex for why the negated class is usually the best choice.

No word boundaries

cat matches inside category and concatenate. Use \bcat\b. Boundaries treat letters, digits and underscores as word characters, so \bid\b still matches the id in user-id, because the hyphen is not a word character.

An unescaped dot

example.com also matches example-com and exampleXcom. It rarely causes harm in a search, but in a replace across thousands of files it can. Write example\.com.

Alternation without a group

^cat|dog$ means "cat at the start" or "dog at the end", not "cat or dog as the whole line". Group the alternatives: ^(cat|dog)$.

Symptom 3: ^ and $ do not work per line

Without multiline mode, ^ and $ only match at the start and end of the whole file. Add (?m) at the start of the pattern, or turn on your tool's multiline option. Editors such as Notepad++ and VS Code already treat each line separately in their own search.

Windows line endings add a second trap. Files saved on Windows end each line with \r\n. In .NET (PowerShell), $ in multiline mode matches just before \n, which is after the \r. So (?m)\s*$ works, but (?m)[ \t]+$ fails on a line that ends with spaces followed by \r\n, because the \r sits between the spaces and the end of the line. Use [ \t]+(?=\r?$) instead. The lookahead lets the optional \r sit between the match and the end of the line.

Symptom 4: $1 appears literally, or the group is empty

What you seeLikely causeFix
The text $1 in the outputRegex mode off, or the tool uses \1Turn regex on; try \1
The text \1 in the outputThe tool uses $1 (VS Code, .NET)Use $1
Group text missing in PowerShellReplacement in double quotes, so $1 was read as an empty variableUse single quotes: '$1'
Wrong text where $10 or $1 + digit should beThe engine read the digit as part of the group numberUse ${1}0 in .NET and PowerShell
A group is always emptyYou counted groups wrongly, or the group is optionalCount opening parentheses from the left, skipping (?: and look-arounds

The PowerShell quoting issue catches nearly everyone once. -replace '(\w+)@', "$1 at " gives an empty result for the group, because PowerShell expands $1 as a variable before the regex engine sees it. Write -replace '(\w+)@', '$1 at '. If you need double quotes, escape the dollar with a backtick: "`$1 at ". The backreference syntax guide lists what each tool expects.

Symptom 5: it works in a regex tester but not in your tool

Online testers default to one flavor, usually PCRE or JavaScript. Windows tools use different engines:

  • PowerShell and .NET apps: .NET regex. Variable-length lookbehind works. Possessive quantifiers such as a++ do not.
  • VS Code: JavaScript regex. Case conversion in the replacement (\U$1) works in VS Code but not in plain JavaScript.
  • Notepad++: Boost. Lookbehind must be fixed length. \K and \R work.

Set the tester to the matching flavor, or at least avoid features that only one engine has.

Symptom 6: the replace works on one file but not on the folder

  • Filters. The file type or folder may be excluded. Check extension filters and whether sub-folders are included. See limiting find and replace to specific file types.
  • Encoding. A file saved as UTF-16 looks like normal text in an editor but can defeat tools that read it as ANSI. Accented characters in the pattern will not match if the file was read in the wrong encoding.
  • Line endings vary. Some files may use \n and others \r\n. Write \r?\n rather than \r\n or \n.
  • Binary or locked files. Files open in another program or marked read-only may be skipped.

A five-step debugging routine

  1. Copy one real line that should match into a test file.
  2. Cut the pattern down to its first literal word and confirm that matches.
  3. Add the pattern back one piece at a time until the match fails. The last piece added is the problem.
  4. Check the replacement separately, starting with $& or $0 (the whole match) to prove the replace runs at all.
  5. Run a search-only pass across the folder and compare the count with what you expect before writing anything.

Bulk Replace makes the last step visual: its before/after diff shows the result of the replacement in every file, with a total match count, before a single file changes. If a group comes out empty or the count is far too high, you see it straight away. Because Bulk Replace backs up every run, a pattern that slipped through can still be undone in one click.

Frequently asked questions

Why does $1 appear literally in my result?

Either regex mode is off, or your tool uses \1 instead of $1. In PowerShell, use single quotes around the replacement so $1 is not treated as a variable.

Why do ^ and $ only match the start and end of the file?

Multiline mode is off. Turn it on, or add (?m) at the start of the pattern, so they match at every line.

Why does my pattern work on a regex website but not in my tool?

Regex testers default to a specific flavor such as PCRE or JavaScript. Pick the flavor your tool uses, for example .NET for PowerShell.

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