问题
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 secondd
inadhd
.
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