Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
vimscript regex:在数组中写入多个匹配项_Regex_Vim - Fatal编程技术网

vimscript regex:在数组中写入多个匹配项

vimscript regex:在数组中写入多个匹配项,regex,vim,Regex,Vim,我为vim编写了一个正则表达式,它应该可以找到文件名。看起来是这样的: let fileMatch = '\([-_a-zA-Z0-9]\+\.\)\+[a-zA-Z0-9]\+' let fileName = matchstr( \ a:input_line, \ fileMatch \ ) 让我们假设a:input\u line的值为: (test/the/plugins.vim:13:2) (test/the/pluginsies.vim:13:2) 我的目标是

我为vim编写了一个正则表达式,它应该可以找到文件名。看起来是这样的:

let fileMatch = '\([-_a-zA-Z0-9]\+\.\)\+[a-zA-Z0-9]\+'
let fileName = matchstr(
    \ a:input_line,
    \ fileMatch
    \ )
让我们假设
a:input\u line
的值为:

(test/the/plugins.vim:13:2) (test/the/pluginsies.vim:13:2)
我的目标是在一个数组中包含
plugins.vim
pluginsies.vim
。 我将在后记中完成该数组

现在,如果我回显文件名,我只会得到第一个匹配项:
plugins.vim

那么,如何在一个数组中保存多个匹配项

最终解决方案
matchstr()
将只返回第一个匹配项。您可以使用循环来获取所有匹配的字符串

我注意到您将忽略带有空格的文件名。因此,这一行可能有助于获得列表中所有匹配的字符串:

let result_list = map(split(a:input_line),'matchstr(v:val, fileMatch)')
它将返回:

['plugins.vim', 'pluginsies.vim'] 

thx,所以我必须先通过空格分隔字符串?不知何故,我可以使用fileMatch正则表达式进行拆分?我读到split()将删除匹配的部分,因此我不能使用我创建的filePathRegex作为拆分模式。我不得不在正则表达式的末尾添加
\zs
,我说,你不必拆分。对于您的示例,split()使之更容易。可以在循环中使用match()/matchstr()来获取所有匹配的字符串。对于第二条评论,您可以使用文件名模式进行拆分,并在split()中包含匹配的字符串。最后,我不喜欢你读答案,然后编辑问题来打破答案(我编辑了示例字符串,以表明它可能更复杂DVIM已经提供了文件标识符的概念
:help isfname
,因此您的regexp可以简化为:
'\f\+
['plugins.vim', 'pluginsies.vim']