PatternRegex

Find blank lines

Locate empty or whitespace-only lines in a file or document, useful for tidying up extra spacing before a diff or a print.

Last updated

Patterngm
^\s*$
Open in Regex Tester & Builder

Matches

  • Line one Line three
  • Para one Para two

Doesn’t match

  • Line one Line two Line three
  • No blank lines All rows have text Final row here

Why it’s written this way

\s*$ says a line may contain zero or more whitespace characters and nothing else, so it matches both a truly empty line and one that's only spaces or a leftover tab.

The m flag redefines ^ and $ to mean the start and end of each line rather than the start and end of the whole string, which is what lets one pattern check every line of a multi-line document in a single pass.

Edge cases to know

  • \s also matches newline characters, so a run of several blank lines back to back can produce more matches than you'd expect from counting them by eye.
  • It treats a line of pure spaces exactly like a truly empty line — if you need to tell those apart, swap \s*$ for a check that excludes newline explicitly.

Related in Patterns