5

使用 vim 脚本,

让我说我想从以下表达式中找到单词“This”

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

我尝试了一些不同的选择getmatches(),,,,substitute()还没有运气:(

有没有办法像在 ruby​​ 或 php 中一样在 vim 中获取匹配项,即matches[1]

- - - - - - - - - - - - - 编辑 - - - - - - - - - - - - -----

from h function-listglts如前所述,我发现了matchlist() distinct matchstr(),它总是返回完整的匹配项,如matches[0],它返回完整的匹配数组。

echo matchstr("foo bar foo", '\vfoo (.*) foo')  " return foo bar foo
echo matchlist("foo bar foo", '\vfoo (.*) foo')  " returns ['foo bar foo', 'bar', '', '', '', '', '', '', '', '']
4

1 回答 1

7

在这种特殊情况下,您可以使用matchstr()(它返回匹配本身,而不是开始位置),并让匹配在before断言之后开始\zs

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

在一般情况下,有matchlist(),它返回整个匹配的列表以及所有捕获的组。结果在第一个捕获组中,因此索引 1 处的元素:

matchlist("testingThis", '\ving(.*)')[1]
于 2013-09-06T06:53:14.543 回答