Sunday, June 19, 2011

Re: remove all except pattern

On 06/19/2011 11:15 AM, rameo wrote:
> I would like to know how to remove everything except pattern.
>
> p.e.
>
> hello this is an example, hello to everybody
> example hello
>
> I do a search for hello
>
> My output has to be:
>
> hellohello
> hello
>
> or
>
> hello hello
> hello

My first thought would be something like

:%s/\w\+/\=submatch(0)=~'pattern'?submatch(0):''

or perhaps

:%s/\w\+\W*/\=submatch(0)=~'pattern'?submatch(0):''

This assumes your pattern is a single-word. You'd have to expand
your search expression to accommodate if it's more than a
single-word. You're basically chunking it up into pieces you can
match your pattern against, and then, if the pattern doesn't
match, replacing it with an empty string ('')

-tim


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: