Reputation: 29
How i can to delete all after last word no.4?
word1 word2 word3 word4 word5
word6 word7 word8 word9
word10 word11 word12 word13 word14
So i want to remove word5 and word14
Upvotes: 1
Views: 205
Reputation: 91385
^(?:\S+\h+){4}\K.*$
LEAVE EMPTY
. matches newline
Explanation:
^ # 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
Result for given example:
word1 word2 word3 word4
word6 word7 word8 word9
word10 word11 word12 word13
Upvotes: 1
Reputation: 3337
You can do it using next pattern for replacement
Search
([\w]+ [\w]+ [\w]+ [\w]+).+$
Replace
\1
Via search we select first four words and put them into a group. All others symbols till the end of the line are not affected
Via replace we put matched symbols instead of the whole line
Upvotes: 0