0

Input

\sn{1}\gn{2}\gn{3}\sn{4}\sn{5}\gn{6}\gn{7}\sn{8}\yr{2012}

Required output

\sn{1}\sn{4}\yr{2012}

I used this code. but failed.

%s/\\sn{\([^}]*\)}.*\\sn{\([^}]*\)}.*\\yr/\sn{\1}\sn{\2}\yr{\3}/gec 
sidyll
  • 57,726
  • 14
  • 108
  • 151
Zam
  • 367
  • 2
  • 17
  • Possible duplicate of [How can I make my match non greedy in vim?](http://stackoverflow.com/questions/1305853/how-can-i-make-my-match-non-greedy-in-vim) – Trevor Boyd Smith Mar 16 '17 at 18:41

1 Answers1

2

the non-greedy in vimregex is .\{-} Take a look :h \{- for details.

So your :s cmd could be written in :

%s/\(\\sn{[^}]*}\).\{-}\(\\sn{[^}]*}\).\{-}\(\\yr{[^}]*}\).*/\1\2\3/g
Kent
  • 189,393
  • 32
  • 233
  • 301