Delete anything other than pattern

前端 未结 3 1415
时光说笑
时光说笑 2020-12-30 04:50

Let\'s say this is my text:

this is my text this
is my text this is my text
my text is this

I would like to highlight all text ex

3条回答
  •  被撕碎了的回忆
    2020-12-30 05:42

    Try this:

    :%s/\(^\|\(text\)\@<=\).\{-}\($\|text\)\@=//g
    

    Explanation:

    \(^\|\(text\)\@<=\)     # means start of line, or some point preceded by “text”
    .\{-}                   # as few characters as possible
    \($\|text\)\@=          # without globbing characters, checking that we reached either end of line or occurrence of “text”.
    

    Another way to do it:

    • Create a function that count matches of a pattern in a string (see :help match() to help you design that)
    • Use: :%s/.*/\=repeat('text', matchcount('text', submatch(0)))

提交回复
热议问题