Bulk Replace
Regex

Regex to Match Everything Between Two Strings, Including Across Lines

Match and replace everything between two markers with regex. Lazy vs greedy, across line breaks, keeping or dropping the markers, with tested examples.

Updated 6 minute read The Bulk Replace team

To match everything between two strings with regex, use START(.*?)END. The .*? is a lazy quantifier, so it stops at the first END rather than the last one, and the parentheses capture the text in the middle as group 1. If the text between the markers can include line breaks, use START([\s\S]*?)END instead, because a plain dot does not match a line break by default. To match only the middle text without the markers, use look-arounds: (?<=START).*?(?=END).

The rest of this guide shows how each version behaves, how to replace the middle while keeping the markers, and the cases where this simple pattern breaks.

The basic pattern, piece by piece

Take the line START a END b END. Here is what different patterns match:

PatternGroup 1 containsWhy
START(.*)END a END b Greedy: runs to the last END on the line
START(.*?)END a Lazy: stops at the first END
START([^E]*)END a Negated class: cannot pass an E at all

The greedy version is the most common mistake. A .* takes as much as it can, then backs up only far enough for the rest of the pattern to match, which means it ends at the final END. Adding ? makes it lazy, so it takes as little as possible. Our guide to greedy vs lazy regex goes into this in more detail.

The negated class version only works when the end marker starts with a character that never appears in the middle text. For single-character delimiters it is the best choice, as shown later.

Matching across line breaks

In most regex engines, including .NET (PowerShell), JavaScript (VS Code) and Boost (Notepad++), the dot does not match a line break unless you change a setting. So START(.*?)END fails when the markers are on different lines. You have three ways around this:

  1. Use [\s\S] instead of the dot. A class containing "whitespace or not whitespace" matches every character, including \r and \n. START([\s\S]*?)END works in every engine without any flag.
  2. Turn on single-line (dotall) mode. Add (?s) at the start of the pattern in .NET and PCRE-style engines, or tick . matches newline in Notepad++. JavaScript uses the s flag.
  3. Use an editor's multiline search. VS Code's search handles \n in the pattern and searches across lines when it sees one.

Option 1 is the most portable, so it is the one used in the examples below. Keep the lazy ? when you span lines: a greedy [\s\S]* can swallow everything from the first start marker to the last end marker in the whole file.

Replace the middle, keep the markers

A common job is to swap out a block between two comment markers, for example a navigation menu that every page wraps like this:

<!-- BEGIN NAV --> ... old menu ... <!-- END NAV -->

Capture the markers and put them back in the replacement:

  • Find: (<!-- BEGIN NAV -->)[\s\S]*?(<!-- END NAV -->)
  • Replace: $1<ul class="nav">...</ul>$2

Group 1 and group 2 hold the markers, so they survive, and everything between them is replaced with the new menu. In tools that use \1 for groups, write \1 and \2 instead. If the new text begins with a digit, write ${1} in .NET, PowerShell and Notepad++ so the engine does not read it as group 10 or higher. The capture groups guide covers this syntax.

Alternatively, match only the middle with look-arounds and replace it directly:

  • Find: (?<=<!-- BEGIN NAV -->)[\s\S]*?(?=<!-- END NAV -->)
  • Replace: the new menu

Look-arounds check the markers without including them in the match. This works in .NET and modern JavaScript. Boost and PCRE allow look-behind only with a fixed-length pattern, which a literal marker is, so this example works there too.

Remove the markers and everything between them

To delete a whole block, including the markers, match the lot and replace with nothing:

RemoveFind
CSS or JS block comments/\*[\s\S]*?\*/
HTML comments<!--[\s\S]*?-->
A marked block<!-- BEGIN ADS -->[\s\S]*?<!-- END ADS -->
A tracking script tag<script\b[^>]*>[\s\S]*?</script>

Note the escaped \* in the comment pattern. An asterisk is a quantifier, so a literal one needs a backslash. The last row removes every script tag, so narrow it with something unique to the one you want, such as its src value.

Removing a block can leave an empty line behind. To take the line break with it, add \r?\n? at the end of the pattern.

Single-character delimiters: quotes, brackets and parentheses

When the start and end markers are single characters, a negated class is clearer and faster than a lazy dot:

Text betweenPatternExample match
Double quotes"([^"]*)"title="Hello world" gives Hello world
Single quotes'([^']*)''en-US' gives en-US
Parentheses\(([^)]*)\)call(a, b) gives a, b
Square brackets\[([^\]]*)\][MUSIC] gives MUSIC
Curly braces\{([^}]*)\}{name} gives name

A negated class such as [^"] also matches line breaks, so these patterns can span lines. If you want to stay on one line, add the line break characters to the class: "([^"\r\n]*)".

The outer brackets and parentheses are escaped because they are special characters in regex. See how to escape special characters in regex if a pattern refuses to match.

Where this pattern breaks

A lazy match between two strings is simple, and it has limits worth knowing before you run it across a folder:

  • Nesting. On [[a] b], the pattern \[(.*?)\] gives [a, not [a] b. Regex cannot count nested pairs. If your markers nest, handle the inner pairs first, or edit those files by hand.
  • A missing end marker. If one file has START with no END after it, there is no match there, which is safe. But if a later block's END is present, the match runs on into the next block. Check unusually long matches in a preview.
  • Markers that appear in normal text. Short markers like " or ( appear everywhere. Make the pattern more specific, for example alt="([^"]*)" rather than "([^"]*)".
  • Case. <!-- begin nav --> does not match <!-- BEGIN NAV --> when match case is on. Decide which you want.
  • Very large files. A lazy [\s\S]*? over a multi-megabyte file with no end marker forces the engine to try a lot of positions. It finishes, but it can be slow.

Running it across many files

Patterns that span lines are exactly the kind that need checking before you apply them to a whole site. Here is a safe routine:

  1. Test the pattern on two or three files that you know well, including one with no markers.
  2. Run a search only, and compare the match count with the number of blocks you expect. One block per page across 200 pages should mean 200 matches.
  3. Look at the longest match. If it covers far more text than a single block, the pattern is greedy or an end marker is missing somewhere.
  4. Replace, then search again for the start marker to confirm every block was updated.

Bulk Replace supports multi-line replacement and regex with capture groups and look-arounds, and it shows a before/after diff of every match in every file before anything is written. You can untick a file where the block looks wrong, and because each run is backed up first, a mistake is one click to undo. For the full process of swapping a repeated block across a static site, see replacing the header or footer on every page.

Quick reference

  • Same line, shortest match: START(.*?)END
  • Across lines: START([\s\S]*?)END
  • Middle only: (?<=START)[\s\S]*?(?=END)
  • Keep markers: find (START)[\s\S]*?(END), replace $1new text$2
  • Single-character delimiters: "([^"]*)", \(([^)]*)\)

Each of these can be pasted straight into a Bulk Replace regex rule. Keep the lazy ? unless you really mean "to the last marker".

Frequently asked questions

What regex matches text between two words?

START(.*?)END matches the shortest text between START and END on one line. Use START([\s\S]*?)END to allow line breaks in between.

How do I match between markers without including them?

Use look-arounds: (?<=START).*?(?=END) matches only the text in the middle, in engines that support look-behind such as .NET and modern JavaScript.

Why does my pattern match too much?

A greedy .* runs to the last END it can reach. Add ? after the quantifier to make it lazy so it stops at the first END.

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