简体   繁体   English

使用Notepad ++删除与正则表达式匹配的所有行(非)

[英]Delete all lines (not) matching a regex using Notepad++

How to delete all lines matching or not matching a regex in Notepad++? 如何在Notepad ++中删除所有匹配或不匹配正则表达式的行?

In Vim, I'd do the following to delete all matching lines: 在Vim中,我会执行以下操作来删除所有匹配的行:

:g/regex/d

And to delete all non matching lines: 并删除所有不匹配的行:

:!g/regex/d

I'm looking for these commands' equivalent in Notepad++. 我在Notepad ++中寻找这些命令。

As also explained in " Notepad++ - delete all lines with certain text ", I usually go for the approach of blanking the matching lines and deleting the blank lines afterwards. 正如在“ Notepad ++ - 删除具有特定文本的所有行 ”中所解释的那样,我通常会采用消隐匹配行并删除空行的方法。 Is there a simpler way? 有更简单的方法吗?


As per this answer , versions of Notepad++ >= 6.0 support matching line breaks in regex, thus allowing to delete whole lines directly without creating blank lines first. 根据这个答案 ,Notepad ++> = 6.0的版本支持正则表达式中的匹配换行符,因此允许直接删除整行而不首先创建空行。 The following pattern should remove all lines containing "foobar" when replaced with an empty string: 当替换为空字符串时,以下模式应删除包含“foobar”的所有行:

^.*foobar.*\r\n

Now, as discussed in " Regular expression to match a line that doesn't contain a word? ", negating regular expressions isn't exactly straightforward. 现在,正如“ 正则表达式匹配不包含单词的行? ”中所讨论的那样,否定正则表达式并不是很简单。 Deleting lines in Notepad++ which do not contain "foobar" would require the following pattern: 删除Notepad ++中不包含“foobar”的行将需要以下模式:

^((?!foobar).)*\r\n

Because that's a quite complicated command to type just for removing lines which don't contain a word (or possibly more complex expression), I wonder if there is an easier solution. 因为这是一个非常复杂的命令,只是为了删除不包含单词的行(或者可能是更复杂的表达式),我想知道是否有更简单的解决方案。

在“ Find对话框的“ Mark选项卡上,您可以为与之匹配的行添加书签...然后在菜单上Search > Bookmark > Remove Unmarked LinesRemove Bookmarked lines

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

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