我怎样才能删除最后一个单词 4 之后的所有内容?
word1 word2 word3 word4 word5
word6 word7 word8 word9
word10 word11 word12 word13 word14
所以我想删除 word5 和 word14
^(?:\S+\h+){4}\K.*$
LEAVE EMPTY
. matches newline
解释:
^ # beginning of line
(?: # start non capture group
\S+ # 1 or more non space character
\h+ # 1 or more horizontal space
){4} # end group, must appear 4 times
\K # forget all we have seen until this position
.* # 0 or more any character but newline
$ # end of line
给定示例的结果:
word1 word2 word3 word4
word6 word7 word8 word9
word10 word11 word12 word13
您可以使用下一个模式进行替换
搜索
([\w]+ [\w]+ [\w]+ [\w]+).+$
代替
\1
通过搜索,我们选择前四个单词并将它们放入一个组中。直到行尾的所有其他符号不受影响
通过替换,我们放置匹配的符号而不是整行