Notepad ++ How to remove all characters after a string

强颜欢笑 提交于 2019-12-21 05:49:28

问题


I'm using regular expression in Notepad++, trying to delete everything after a particular word.

For example here is my text:

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

etc..

Im trying to delete everything after the word Bull, so that my results ends up being just Bull. I thought it was as simple as searching for

Bull.*

but that deletes the whole row, including the word Bull.


回答1:


Use a look behind:

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

The handy thing about look arounds is that they assert a match without consuming anything, so you don't have to sully yourself with back references.




回答2:


You can search for (Bull).* and replace it with $1 - but take care to not set dot matches \r and \n




回答3:


You may use a 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.

So, you match whatever you like (even quantified patterns like \d+ or [a-z]{7,}), then insert \K and add .+ to grab the rest of the line (1 or more characters other than line break chars):

Bull\K.+

Then all what remains is to use an empty string in the replacement pattern.

See the Notepad++ screenshot:



来源:https://stackoverflow.com/questions/35807579/notepad-how-to-remove-all-characters-after-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!