5

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.

Community
  • 1
  • 1
n34_panda
  • 2,577
  • 5
  • 24
  • 40

1 Answers1

10

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?
Community
  • 1
  • 1
Sam
  • 20,096
  • 2
  • 45
  • 71
  • Yeah I have reg ex selected under search mode - I have tried your sample within notepad++ at it works...unfortunately if I implement it against my file it doesn't. Just states 0 occurences was replaced. The content per line varies but I assume (without understanding the expression - googling it now) it doesn't matter – n34_panda May 26 '14 at 17:49
  • No it shouldn't matter. The original expression should've worked fine -- the main thing was the search mode. – Sam May 26 '14 at 17:50
  • Thought so - I've tried re-creating my text file and it just doesn't work...I'm using notepad++ 6.5.5 I assume thats pretty up-to-date? (updatin t now) – n34_panda May 26 '14 at 17:50
  • Yea, that came out 2 months ago according to their page. I'm on Linux and don't have Wine on this machine so I can't try it out in N++, but if you want to update your question with an uploaded screenshot of your find/replace window I can look for an issue. – Sam May 26 '14 at 17:52
  • I'll switch to Linux myself and see how I get on. Weirdly, I tried copying and pasting my text into another new file and it worked first time....regex/replace was fine - just notepad++ being stupid? – n34_panda May 26 '14 at 17:53
  • @n34_panda actually seems like Windows being "stupid", see my update. – Sam May 26 '14 at 17:56
  • 1
    marked as answered - cheers! Decided to use my linux and sed to avoid it happening again. – n34_panda May 26 '14 at 17:57
  • 2
    +1 Sam, but since N++ uses PCRE, you can replace `[\r\n]` with `\R`, and `[^\r\n]` with `[^\R]` :) – zx81 May 26 '14 at 21:11
  • Good catch @zx81, I didn't have N++ to test PCRE so I played it safe. – Sam May 26 '14 at 21:24
  • @Sam Yes, I was high when I wrote the `[^\R]`... :) `(?!\R).` works there but doesn't save any chars. – zx81 May 26 '14 at 21:32
  • The entire `\r` thing is superfluous. The `\r` will be matched by the `[^\n]` part, so it will be either copied to the result, or thrown away with the `\n`. It wasn't actually an oversight in the linked answer, but rather a conscious improvement (since the original regex works with both line endings). – Jasper May 21 '15 at 09:29