简体   繁体   English

如何使用 Notepad++ 正则表达式删除单个空行

[英]How to Remove Single Blank Line Using Notepad++ Regex

I want to remove just the last line in a file if it's a line containing nothing.如果它是一个不包含任何内容的行,我只想删除文件中的最后一行。 I am using Notepad++.我正在使用记事本++。

Details细节

Say I have a text file open in Notepad++.假设我在 Notepad++ 中打开了一个文本文件。 If the last line of a file is the empty string, I want to remove it.如果文件的最后一行是空字符串,我想删除它。 I want to do this using Notepad++'s native find/replace functionality, with regexes.我想使用 Notepad++ 的本机查找/替换功能和正则表达式来做到这一点。

Use Case用例

I often encounter this case after I copy a file from Visual Studio to Notepad++.我在将文件从 Visual Studio 复制到 Notepad++ 后经常遇到这种情况。 The reason for searching how to do it with a regex is: when recording a macro in Notepad++ it's more stable to use a regex find/replace than just manually deleting a character.搜索如何使用正则表达式进行操作的原因是:在 Notepad++ 中录制宏时,使用正则表达式查找/替换比手动删除字符更稳定。 The manual delete might be sensitive to file length.手动删除可能对文件长度敏感。

解决方案是搜索此正则表达式: \\r\\n$(?!\\r\\n)并将其替换为空字符串(空字符串)。

  • Ctrl + H Ctrl + H
  • Find what: \\R\\z找到什么: \\R\\z
  • Replace with: LEAVE EMPTY替换为: LEAVE EMPTY
  • CHECK Regular expression检查正则表达式
  • Replace代替

Explanation:解释:

\R       # any kind of linebreak (i.e. \r, \n, \r\n)
\z       # end of file

Screen capture (before):屏幕截图(之前):

在此处输入图片说明

Screen capture (after):屏幕截图(之后):

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM