JeekZ
JeekZ

Reputation: 29

How i can to delete all after last word no.x?

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

Answers (2)

Toto
Toto

Reputation: 91385

  • Ctrl+H
  • Find what: ^(?:\S+\h+){4}\K.*$
  • Replace with: LEAVE EMPTY
  • check Wrap around
  • check Regular expression
  • UNCHECK . matches newline
  • Replace all

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 

Before:

enter image description here

After:

enter image description here

Upvotes: 1

Pavlo Zhukov
Pavlo Zhukov

Reputation: 3337

You can do it using next pattern for replacement

Search

([\w]+ [\w]+ [\w]+ [\w]+).+$

Replace

\1

Recorded screencast

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

Related Questions