Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting every 2nd line from a file using Notepad++

I am looking for some regex help.

I have a textfile, nothing super important but I would like to delete every second line from it - I have tried following this guide: Delete every other line in notepad++

However I just can't get it to work, is the regex I am using ok? I am noob with regex

Find:

([^\n]*\n)[^\n]*\n

Replace with:

$1

No matter what I try (mouse position at the beginning, ctrl+a and Replace All) I just can't get it to work. I appreciate any help.

I've put the regex into here: http://regexpal.com/ and if I remove the final \n it highlights the individual rows.

like image 675
n34_panda Avatar asked Feb 12 '23 21:02

n34_panda


1 Answers

Make sure you select regular expression for the search mode...

Also, you may want to make that final newline optional. In the case that there are an even number of lines and you do not have a trailing newline, it won't remove the last line.

([^\n]*\n)[^\n]*\n?

Update:

See how Windows handle new lines with \r\n instead of just \n. Try updating the expression to take this into account:

([^\r\n]*[\r\n]+)[^\r\n]*[\r\n]*

Final Update:

Thanks to @zx81, I now know that N++ uses PCRE so \R can be used for unicode newline characters. However [^\R] won't work (this looks for anything except R literally), so you will need to keep [^\r\n]. This can be simplified as:

([^\r\n]*\R)[^\r\n]*\R?
like image 114
Sam Avatar answered Feb 15 '23 10:02

Sam