加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

迭代正则表达式结果vimscript

发布时间:2020-12-14 05:46:56 所属栏目:百科 来源:网络整理
导读:在 vimscript中,如何遍历当前文件中正则表达式的所有匹配项,然后为每个结果运行 shell命令? 我认为这是一个开始,但我无法弄清楚如何将它提供给整个文件并获得每场比赛. while search(ENTIRE_FILE,".*{{zs.*ze}}",'nw') 0 system(do something with THIS_M
在 vimscript中,如何遍历当前文件中正则表达式的所有匹配项,然后为每个结果运行 shell命令?

我认为这是一个开始,但我无法弄清楚如何将它提供给整个文件并获得每场比赛.

while search(ENTIRE_FILE,".*{{zs.*ze}}",'nw') > 0
    system(do something with THIS_MATCH)
endwhile

解决方法

假设我们有一个包含内容的文件:

123 a shouldmatch
456 b shouldmatch
111 c notmatch

我们喜欢匹配

123 a shouldmatch
456 b shouldmatch

与正则表达式

.*shouldmatch

如果每行只有一个匹配项,则可以使用readfile(),然后遍历这些行并使用matchstr()检查每一行. [1]

function! Test001()
  let file = readfile(expand("%:p")) " read current file
  for line in file
    let match = matchstr(line,'.*shouldmatch') " regex match
    if(!empty(match))
      echo match
      " your command with match
    endif
  endfor
endfunction

您可以将此函数放在?/ .vimrc中,并通过调用Test001()调用它.

[1] http://vimdoc.sourceforge.net/htmldoc/eval.html#matchstr%28%29

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读