【问题标题】:How to find a match between specific columns?如何在特定列之间找到匹配项?
【发布时间】:2018-03-02 21:51:38
【问题描述】:

文本文件中的示例第 7 行:

This is an example

这是我的搜索代码:

/example\|an

现在我想捕获从第 1 列到第 11 列的子字符串中的匹配项:

let myhits = [] | 7s/\%>0cexample\|an\%<12c/\=add(myhits,submatch(0))[-1]/ge | echo myhits

我预计:

myhits = ["an"]  

但发现:

myhits = ["an", "example"] 

我做错了什么?
如何捕获子字符串中的匹配项?

【问题讨论】:

    标签: string search vim substring


    【解决方案1】:

    \| 分隔的所有内容都是它自己的分支,因此在您的表达式中,左分支不检查结束列,右分支不检查结束列。您可以使用分组仅在关键字上进行分支:\%&gt;0c\(example\|an\)\%&lt;12c

    【讨论】:

    • 谢谢!不幸的是,就我而言,并不总是可以对我的搜索进行分组。捕获子字符串并在之后仅在子字符串中进行搜索是否是一种解决方案?
    • 是的,这听起来是个好主意。您可以使用let myLine = getline(7)[0:10],然后编写类似于伪代码while (length(myLine) &gt; 0) {call substitute(pattern, "", myLine) for each pattern and add to myhits if myLine changed} 的内容。我无法想到 vim 语法,但 help function-listhelp substitute 可能会有所帮助。
    猜你喜欢
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2011-06-28
    • 2022-12-17
    • 2021-07-12
    • 2017-01-25
    相关资源
    最近更新 更多