问题描述
在Vim中,有没有一种方法可以搜索与说abc
相匹配的行,但以后在该行中也不包含xyz
?因此,以下几行将匹配:
In Vim, is there a way to search for lines that match say abc
but do not also contain xyz
later on the line? So the following lines would match:
The abc is the best
The first three letters are abc
,以下内容不匹配:
The abc is the best but xyz is cheaper
The first three letters are abc and the last are xyz
我了解如下语法:
/abc\(xyz\)\@!
,但这只会避免匹配abcxyz
,如果两者之间没有任何东西(例如abc-xyz
),则不会.使用
but that only avoids matching abcxyz
and not if there is anything in between, such as abc-xyz
. Using
/abc.*\(xyz\)\@!
也不起作用,因为该行后面的许多位置xyz
不匹配.
also does not work because there are many positions later in the line where xyz
is not matched.
(我应该注意,在命令行上我会做类似grep abc <infile | grep -v xyz
的操作,但是我想在Vim中以交互方式执行上述操作.)
(I should note that on the command line I would do something like grep abc <infile | grep -v xyz
but I would like to do the above interactively in Vim.)
推荐答案
您的尝试非常接近;您需要拉动.*
,以允许匹配和断言的以后的不匹配之间的任意距离进入否定的预测:
Your attempt was pretty close; you need to pull the .*
that allows an arbitrary distance between the match and the asserted later non-match into the negative look-ahead:
/abc\(.*xyz\)\@!
我猜这是可行的,因为尝试对.*
的所有可能匹配都进行不匹配,并且只有当所有分支都用完后,\@!
才声明为已实现.
I guess this works because the non-match is attempted for all possible matches of .*
, and only when all branches have been exhausted is the \@!
declared as fulfilled.
这篇关于有没有办法在vim regex中进行负前瞻?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!