使用Notepad ++从文件中删除每一行

时间:2022-12-03 19:25:14

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

但是我无法让它工作,我使用的正则表达式是否正常?我是正则表达式的noob

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.

无论我尝试什么(开头的鼠标位置,ctrl + a和全部替换)我都无法让它工作。我感谢任何帮助。

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

我把正则表达式放在这里:http://regexpal.com/如果我删除最终\ n它会突出显示各行。

1 个解决方案

#1


8  

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:

了解Windows如何使用\ r \ n而不是\ n来处理新行。尝试更新表达式以将此考虑在内:

([^\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:

感谢@ zx81,我现在知道N ++使用PCRE所以\ R可以用于unicode换行符。但是[^ \ R]不起作用(这样可以查找除R之外的任何内容),因此您需要保留[^ \ r \ n]。这可以简化为:

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

#1


8  

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:

了解Windows如何使用\ r \ n而不是\ n来处理新行。尝试更新表达式以将此考虑在内:

([^\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:

感谢@ zx81,我现在知道N ++使用PCRE所以\ R可以用于unicode换行符。但是[^ \ R]不起作用(这样可以查找除R之外的任何内容),因此您需要保留[^ \ r \ n]。这可以简化为:

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