Saturday, January 18, 2014

Re: Copy the matched substring only with :g

On 2014-01-15 21:19, Christian Brabandt wrote:
> Hi Tim!
>
> On Mi, 15 Jan 2014, Tim Chase wrote:
>
> > On 2014-01-15 15:29, Paul Isambert wrote:
> > > g/\a*\(\l\u\|\u\l\)\a*/call setline(line('$')+1,
> > > matchstr(getline('.'), '\a*\(\l\u\|\u\l\)\a*'))
> >
> >
> > Riffing off Paul's solution, I'd suggest
> >
> > :g/\<\w*\l\u\w*\>/call setline(line('$')+1,
> > matchstr(getline('.'), @/))
> >
> > which removes the need to specify the pattern twice, using the "/"
> > register to specify it to matchstr.
> >
> > The regexp can be tweaked (I chose the above as my "there's
> > something camelCase" regexp), but there are cases you'd have to
> > make a determination on camelCase'ness:
> >
> > HTMLobject
> > MyObject
> > _myProperty
> > _MyObject
> > InnerHTML
> > THIS_IS_HTML
>
> That still doesn't get all matches per line. Here is a different
> approach (that needs some 7.3.600 patch or so):
>
> :let a=[]
> :%s/pattern/\=add(a,submatch(0))/gn
> :call append('$', a)
>
> Use whatever pattern you need.

Nice. To get it to work in pre-7.3.600 (I believe you're talking
about the "evaluate the replacement with the 'n' flag" discussion
from a while back), you can use

:let a=[]
:%s/pattern/\=empty(add(a, submatch(0)))?'':submatch(0)/g

It will alter 'modified', but at least it should be a noop for your
document.

-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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: