用vim脚本,
让我说我想从下面的表达式中找到"This"这个词
match("testingThis", '\ving(.*)')
我尝试了一些不同的选项,getmatches(),substitute(),不是运气还:(
有没有办法在vim中获得匹配,如ruby或php,即 matches[1]
- - - - - - - - - - - - - 编辑 - - - - - - - - - - - - -----
来自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', '', '', '', '', '', '', '', '']
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,您可以使用matchstr()(返回匹配本身,而不是起始位置),并让匹配在before -ssertion 之后开始\zs:
matchstr("testingThis", '\ving\zs(.*)')
Run Code Online (Sandbox Code Playgroud)
在一般情况下,matchlist()会返回整个匹配的列表以及所有捕获的组.结果是在第一个捕获组中,因此索引1处的元素:
matchlist("testingThis", '\ving(.*)')[1]
Run Code Online (Sandbox Code Playgroud)