Notepad++如何去掉一个字符串后面的所有字符

Notepad ++ How to remove all characters after a string

我在 Notepad++ 中使用正则表达式,试图删除特定单词后的所有内容。

例如这是我的文字:

Bull01 blah blah
Bull02 Blah blah
Bull03 Blah
Bull04 Blah
Bull05 Blah
**
Bull300 Blah blah blah

等等

我试图删除单词 Bull 之后的所有内容,这样我的结果就只有 Bull。我认为这就像搜索

一样简单
Bull.*

但这会删除整行,包括单词Bull

您可以搜索 (Bull).* 并将其替换为 - 但请注意 而不是 设置点匹配 \r 和 \n

使用回头看看:

Search: (?<=Bull).*
Replace: <blank>

look arounds 的便利之处在于它们在不消耗任何东西的情况下断言匹配,因此您不必用反向引用来玷污自己。

您可以使用 match reset operator \K:

\K keeps the text matched so far out of the overall regex match. h\Kd matches only the second d in adhd.

所以,你可以匹配任何你喜欢的(甚至是像 \d+[a-z]{7,} 这样的量化模式),然后插入 \K 并添加 .+ 来获取其余的行(换行符以外的 1 个或多个字符):

Bull\K.+

那么剩下的就是在替换模式中使用一个空字符串。

查看 Notepad++ 截图: