Alfie LC
Alfie LC

Reputation: 17

Replace every second blank line in Notepad++

Is there a way in Notepad++ to replace every other blank line? The number of filled lines is random and so is the occurrence of empty lines.

Is this differentiation possible with a non-uniform spacing or is it outwith the scope of expressions in Notepad?

EDIT: I'm looking to replace every second occurrence of a blank line.

For example:-

    Bed
    Bee

    [BLANK A]

    Bible
    Bible
    Bird
    Bomb

    [BLANK B]

    Book
    Boss
    Bottle
    Bowl
    Box

    [BLANK A]

    Boy
    Brain
    Bridge
    Butterfly
    Button
    Cappuccino
    Car

    [BLANK B]

    Car-race
    Carpet
    Carrot
    Cave
    Chair

    [BLANK A]

    Chess Board
    Chief
    Child
    Chisel
    Chocolates

Where A and B should be replaced separately.

Upvotes: 0

Views: 385

Answers (2)

Toto
Toto

Reputation: 91430

  • Ctrl+H
  • Find what: ((?:.+\R)+)(\R)(?:((?:.+\R)+)(\R))?
  • Replace with: $1[BLANK A]$2(?3$3[BLANK B]$4)
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(               # group 1 (will contain consecutive non blank lines)
  (?:           # non capture group
    .+          # 1 or more any character but newline
    \R          # any kind of linebreak (i.e. \r, \n, \r\n)
  )+            # end group, may appear 1 or more times
)               # end group 1
(\R)            # group 2, any kind of linebreak (i.e. first blank line)
(?:             # non capture group
  (             # group 3, same pattern as in group 1
    (?:
      .+
      \R
    )+
  )
  (\R)          # group 4, any kind of linebreak (i.e. second blank line)
)?              # end group, optional

Replacement:

$1              # content of group 1 (non blank lines)
[BLANK A]       # replacement for first blank line
$2              # content of group 2, (a linebreak)
(?3             # if group 3 exists:
    $3              # content of group 3 (non blank lines)
    [BLANK B]       # replacement for second blank line
    $4              # content of group 4, (a linebreak)
)               # end condition

Screen capture (before):

enter image description here

Screen capture (after):

enter image description here

Upvotes: 1

evilmandarine
evilmandarine

Reputation: 4553

Menu Search > Replace...

Find what: ^(\s+)*\n

Replace: (leave empty)

Set Radio button to regular expression.

This should replace all blank lines independently of how many spaces there are.

Upvotes: 0

Related Questions