Fortmann
Fortmann

Reputation: 443

Regex to replace multiple blank lines

Why does the following pattern not match only two or more consecutive blank lines? (Including regex flag : Multiline)

/(^\s*$){2,}/m

Using Regex101, I see that it matches (for example) the first single blank line of example below (note, I did use ALT-255 for the first character in the block quote below just to represent a starting blank line, remove it if you copy the example text):

 
 some text after the first blank line
 more text
  // comment after a space


 // comment after 2 blank lines 
 text
 // comment

 // comment

How can I tweak this to match 2 or more blank lines only?

Upvotes: 1

Views: 2422

Answers (3)

Tim M.
Tim M.

Reputation: 155

If you also want to include 2 or more consecutive blank lines which may or may not have whitespace characters in the lines:

(^\s*\n{2,})

regex demo

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

  ^(\n{2,})

Here is the working DEMO

Upvotes: 1

user2705585
user2705585

Reputation:

Regex you should be using is ^\n{1,}$

This will look for 2 or more blank newlines.

Regex101 Demo

Upvotes: 4

Related Questions