Home > other >  Regex to identify paragraph in vim consisting of exactly 1 row
Regex to identify paragraph in vim consisting of exactly 1 row

Time:11-18

Input

row 1
row 2

row 1

row 1
row 2
row 3
row 4
row 5
row 6

row 1
row 2
row 3
row 4
row 5

The regex should only return the paragraph consisting only of row 1

row 1

I tried something like v/(\n\n) /d for the first, but didn't get it to work.

CodePudding user response:

You can use

:%s/\v\n*. (\n. ) \n*/\r/g

This command replaces all occurrences in the file of the following

  • \v - enables very magic mode to avoid escaping hell
  • \n* - matches zero or more line breaks
  • . - one or more chars other than line break chars (a non-empty line)
  • (\n. ) - matches one or more non-empty lines
  • \n* - zero or more newlines.

The replacement is a line break, \r, to avoid line concatenation if there are several matches.

  • Related