vim,如何在正则表达式的括号中得到匹配的单词

时间:2013-09-05 17:24:49

标签: vim vim-plugin

使用vim脚本,

我想说我想从下面的表达式中找到“This”这个词

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

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

是否有办法在vim中获取匹配,如ruby或php,即matches[1]

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

来自h function-list,提到glts,我发现matchlist() 不像matchstr(),它总是返回完整的匹配,比如匹配[0],它返回完整的匹配数组。

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

1 个答案:

答案 0 :(得分:5)

在这种特殊情况下,您可以使用matchstr()(返回匹配本身,而不是起始位置),然后让匹配在之前 -assertion {{1 }}:

\zs

在一般情况下,有matchstr("testingThis", '\ving\zs(.*)') ,它返回整个匹配的列表以及所有捕获的组。结果是在第一个捕获组中,因此索引为1的元素:

matchlist()
相关问题