tags:

views:

66

answers:

3

I have a file whose content is roughly something like

insert into FooBar values (13, 19, 220, 108);
// some text,
// some more text
insert into MixMax values (22,  5, 87, 1);
// and so on
insert into HooHoo values (8, 37, 222, 51);
// etc ...

Now, I'd like to yank the first numbers after the string values ( into a variable, preferably in one go.

I thought of something like :g/values (\(\d\+\)/let @a .= ', ' . submatch(1) and would then have expected the register a to be ,13,22,8.

Unfortunately, with this method, I only get the commas, but not the submatches, i.e. the register's content is ,,,

What can I do to solve this problem?

+2  A: 

First of all, submatch() only works with :substitute.

I think this is what you want. There will be an extra comma on the end.

:let @a=''
:g/values (/normal f(l"Ayf,
:let @a=substitute(@a, ',', ', ', 'g')
graywh
Actually, that would be `:g/values (/normal f(l"Ayf,`, OP only wanted the first number from each value set.
wich
Thanks for your answer. Together with wich's correction this is what I was after.
René Nyffenegger
A: 

What's the problem with yi( ?

Bryan Ross
+1  A: 

If you wanted to do it on one line, you could do something like this:

let @a = '' | g/^/let @a .= matchstr(getline('.'), 'values (\zs\d\+, \ze')

The :g part runs the rest on every line (you can filter if you want, but matchstr is doing that anyway). matchstr returns the matching string from the current line. The \zs and \ze delimit the actual match, so that you don't get the 'values (' bit.

See:

:help matchstr()
:help getline()
:help \zs
:help \ze
:help :bar
Al