使用 Notepad++ 删除所有(不)匹配正则表达式的行 [英] Delete all lines (not) matching a regex using Notepad++

查看:319
本文介绍了使用 Notepad++ 删除所有(不)匹配正则表达式的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Notepad++ 中删除所有匹配或不匹配正则表达式的行?

How to delete all lines matching or not matching a regex in Notepad++?

在 Vim 中,我会执行以下操作来删除所有匹配的行:

In Vim, I'd do the following to delete all matching lines:

:g/regex/d

并删除所有不匹配的行:

And to delete all non matching lines:

:!g/regex/d

我正在 Notepad++ 中寻找这些命令的等效项.

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. Is there a simpler way?

根据 这个答案,Notepad++ >= 6.0 版本支持匹配正则表达式中的换行符,从而允许删除整个直接行而不先创建空行.当替换为空字符串时,以下模式应删除所有包含foobar"的行:

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. The following pattern should remove all lines containing "foobar" when replaced with an empty string:

^.*foobar.*\r\n

现在,如正则表达式中所述匹配不包含单词的行?",否定正则表达式并不完全简单.在 Notepad++ 中删除不包含foobar"的行需要以下模式:

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:

^((?!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 >书签 >删除未标记的行删除书签的行

In the Find dialog, on the Mark tab, you can bookmark lines that match... Then on the menu Search > Bookmark > Remove Unmarked Lines or Remove Bookmarked lines

这篇关于使用 Notepad++ 删除所有(不)匹配正则表达式的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆