views:

274

answers:

2

I'd like to be able to, say, check the value of $1 inside the if block, to get the first backreference. However vimscript is not perl.

Is there some way to do this? I'm aware of the possibility of using \1, \2, etc., inside the regex, but I'd like to match and then use the values in subsequent statements, as is possible in perl, php, etc.

For example it would be really nice to have in the evaluated part of g// commands, so you could do things like

:g/number: \(\d\+\)/b:number += v:matches[1] " or whatever the syntax is/could be

to total the values of some number in a file.

Can this be done?

If not, is there a good reason?

+1  A: 

matchlist({expr}, {pat}[, {start}[, {count}]])

Returns \1,\2,\3, etc as found when searching for {pat} in {expr}

Example:

echo matchlist('acd', '(a)\?(b)\?(c)\?(.*)')

Results in: ['acd', 'a', '', 'c', 'd', '', '', '', '', '']

Antony Hatchkins
Wow, that was pretty obvious, not sure why I missed that when scanning the help. Note to self: RTFM more thoroughly. Thanks!
intuited
you're welcome :)
Antony Hatchkins
+1  A: 

In further revelations I've done :lhelpgrep submatch | lope and discovered at least one other useful facility, that you can use submatch({nr}) from the evaluated side of a substitute command. EG

:s/there a\(r\)e n\(o \)stupid questions, on\(ly\) stupid people/\=toupper(submatch(2).submatch(1).submatch(3)."?")/

I tried this approach before but I think I was searching for /backref/ and didn't get the info I was looking for.

intuited