6

with vim script,

Let me say that I want to find word "This" from the following expression

match("testingThis", '\ving(.*)')

I tried with some different options, getmatches(), substitute(), not luck yet :(

Is there a way get matches in vim like in ruby or php, i.e. matches[1]

--------------------------EDIT----------------------------

from h function-list, as glts mentioned, I found matchlist() unlike matchstr(), which always returns full matches, like matches[0], it returns full array of matches.

echo matchstr("foo bar foo", '\vfoo (.*) foo')  " return foo bar foo
echo matchlist("foo bar foo", '\vfoo (.*) foo')  " returns ['foo bar foo', 'bar', '', '', '', '', '', '', '', '']
allenhwkim
  • 27,270
  • 18
  • 89
  • 122

1 Answers1

8

In this particular case, you can use matchstr() (which returns the match itself, not the start position), and let the match start after the before-assertion with \zs:

matchstr("testingThis", '\ving\zs(.*)')

In the general case, there's matchlist(), which returns a List of the entire match plus all captured groups. The result is in the first capture group, so the element at index 1:

matchlist("testingThis", '\ving(.*)')[1]
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324