Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete every third line on Notepad++?

I have text on new lines like so:

tom
tim
john
will
tod
hello
test
ttt
three

I want to delete every third line so using the example above I want to remove: john,hello,three

I know this calls for some regex, but I am not the best with it!

What I tried:

Search: ([^\n]*\n?){3} //3 in my head to remove every third
Replace: $1

The others I tried were just attempts with \n\r etc. Again, not the best with regex. The above attempt I thought was kinda close.

like image 939
William Avatar asked Dec 15 '22 00:12

William


1 Answers

This will delete every third line that may contain more than one word.

  • Ctrl+H
  • Find what: (?:[^\r\n]+\R){2}\K[^\r\n]+(?:\R|\z)
  • Replace with: LEAVE EMPTY
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

(?:             # start non capture group
  [^\r\n]+      # 1 or more non linebreak
  \R            # any kind of linebreak (i.e. \r, \n, \r\n)
){2}            # end group, appears twice (i.e. 2 lines)
\K              # forget all we have seen until this position
[^\r\n]+        # 1 or more non linebreak
(?:             # start non capture group
  \R            # any kind of linebreak (i.e. \r, \n, \r\n)
 |              # OR
  \z            # end of file
)               #end group

Result for given example:

tom
tim
will
tod
test
ttt

Screen capture:

enter image description here

Demo

like image 183
Toto Avatar answered Dec 26 '22 21:12

Toto