在vim中查找完全匹配 [英] Finding exact match in vim
问题描述
使用 /
或 ?
可以在 vim 中找到一个单词的匹配项.但是我怎样才能找到一个完全匹配的呢?例如,我的文本包含以下单词: a aa aaa aaaa aa
并且我输入 /aa
这将找到包含模式 aa 的所有字符串,但是如果我想要怎么办准确地找到 aa
而不是 aaaa aaaa
?
Using /
or ?
enables to find a match for a word in vim. But how can I find an exact match?
For example, my text contains the following words: a aa aaa aaaa aa
and I type /aa
This will find all the strings containing the pattern aa, but what if I want to find exactly aa
and not aaaa aaaa
?
推荐答案
找到一个独立的 aa
(不是 a
, aaa
, ...),您需要在前后都断言该字符不匹配.这是通过负lookbehind(在Vim的正则表达式语法中为\@)和lookahead(
\@!
code>) 封闭匹配本身 (a\{2}
):
To find a single, standalone aa
(not a
, aaa
, ...), you need to assert no match of that character both before and afterwards. This is done with negative lookbehind (\@<!
in Vim's regular expression syntax) and lookahead (\@!
) enclosing the match itself (a\{2}
):
/\%(a\)\@<!a\{2}\%(a\)\@!/
简化
那些断言很难输入,如果匹配周围的边框也是非关键字/关键字边框,你可以使用较短的\<\a\{2}\>
断言(如在 MosteM 的回答中),但这在一般情况下不起作用,例如使用 xaax
.
Simplification
Those assertions are hard to type, if the border around the match is also a non-keyword / keyword border, you can use the shorter \<\a\{2}\>
assertions (as in MosteM's answer), but this doesn't work in the general case, e.g. with xaax
.
这篇关于在vim中查找完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!