Pretty basic question, I'm trying to write a regex in Vim to match any phrase starting with "abc "
directly followed by anything other than "defg"
.
非常基本的问题是,我试图在Vim中编写一个regex,以匹配任何以“abc”开头的短语,然后是除“defg”之外的任何短语。
I've used "[^defg]"
to match any single character other than d, e, f or g.
我使用“^ defg]”匹配任何单个字符以外的d,e,f,g。
My first instinct was to try /abc [^\(defg\)]
or /abc [^\<defg\>]
but neither one of those works.
我的第一反应是尝试/ abc[^ \(defg \)]或/ abc(^ \ < defg \ >),但没有一个这样的工作。
4 个解决方案
#1
41
Here's the search string.
这是搜索字符串。
/abc \(defg\)\@!
The concept you're looking for is called a negative look-ahead assertion. Try this in vim for more info:
你正在寻找的概念被称为消极的前瞻性断言。在vim中试试这个,了解更多信息:
:help \@!
#2
14
preceeded or followed by?
进展成为或紧随其后的是吗?
If it's anything starting with 'abc ' that's not (immediately) followed by 'defg', you want bmdhacks' solution.
如果以abc开头而不是紧接着的defg,你需要bmdhacks'解决方案。
If it's anything starting with 'abc ' that's not (immediately) preceeded by 'defg', you want a negative lookbehind:
如果任何以abc开头的词都没有(立即)被排在“defg”之前,那么你需要一个负面的印象:
/\%(defg\)\@<!abc /
This will match any occurance of 'abc ' as long as it's not part of 'defgabc '. See :help \@<!
for more details.
这将匹配任何出现的“abc”,只要它不是“defgabc”的一部分。看到:帮助\ @ < !为更多的细节。
If you want to match 'abc ' as long as it's not part of 'defg.*abc ', then just add a .*
:
如果你想匹配abc,只要它不属于defg。*abc ',然后加上a。*:
/\%(defg.*\)\@<!abc /
Matching 'abc ' only on lines where 'defg' doesn't occur is similar:
仅在没有出现“defg”的行上匹配“abc”是相似的:
/\%(defg.*\)\@<!abc \%(.*defg\)\@!/
Although, if you're just doing this for a substitution, you can make this easier by combining :v//
and :s//
虽然,如果只是为了替换,可以通过合并:v//和:s//来简化
:%v/defg/s/abc /<whatever>/g
This will substitute '<whatever>' for 'abc ' on all lines that don't contain 'defg'. See :help :v
for more.
这将在所有不包含“defg”的行中将“
#3
0
Here we go, this is a hairy one:
这是一个多毛的
/\%(\%(.\{-}\)\@<=XXXXXX\zs\)*
(replace XXXXXX with the search word). This will search for everything that does not contain XXXXXX. I imagine if you did:
(用搜索词替换XXXXXX)。这将搜索不包含XXXXXX的所有内容。我想如果你这样做了:
/abc \%(\%(.\{-}\)\@<=defg\zs\)*
This may work like you want it to. Hope this helps a little!
这可能是你想要的。希望这能有所帮助!
#4
-4
/abc\ [^d][^e][^f][^g]
e / abc \[^ d][^][^ f][^ g]
It's pretty cumbersome for larger words, but works like a charm.
对于大一点的词来说,这是相当麻烦的,但它的作用就像一种魅力。
#1
41
Here's the search string.
这是搜索字符串。
/abc \(defg\)\@!
The concept you're looking for is called a negative look-ahead assertion. Try this in vim for more info:
你正在寻找的概念被称为消极的前瞻性断言。在vim中试试这个,了解更多信息:
:help \@!
#2
14
preceeded or followed by?
进展成为或紧随其后的是吗?
If it's anything starting with 'abc ' that's not (immediately) followed by 'defg', you want bmdhacks' solution.
如果以abc开头而不是紧接着的defg,你需要bmdhacks'解决方案。
If it's anything starting with 'abc ' that's not (immediately) preceeded by 'defg', you want a negative lookbehind:
如果任何以abc开头的词都没有(立即)被排在“defg”之前,那么你需要一个负面的印象:
/\%(defg\)\@<!abc /
This will match any occurance of 'abc ' as long as it's not part of 'defgabc '. See :help \@<!
for more details.
这将匹配任何出现的“abc”,只要它不是“defgabc”的一部分。看到:帮助\ @ < !为更多的细节。
If you want to match 'abc ' as long as it's not part of 'defg.*abc ', then just add a .*
:
如果你想匹配abc,只要它不属于defg。*abc ',然后加上a。*:
/\%(defg.*\)\@<!abc /
Matching 'abc ' only on lines where 'defg' doesn't occur is similar:
仅在没有出现“defg”的行上匹配“abc”是相似的:
/\%(defg.*\)\@<!abc \%(.*defg\)\@!/
Although, if you're just doing this for a substitution, you can make this easier by combining :v//
and :s//
虽然,如果只是为了替换,可以通过合并:v//和:s//来简化
:%v/defg/s/abc /<whatever>/g
This will substitute '<whatever>' for 'abc ' on all lines that don't contain 'defg'. See :help :v
for more.
这将在所有不包含“defg”的行中将“
#3
0
Here we go, this is a hairy one:
这是一个多毛的
/\%(\%(.\{-}\)\@<=XXXXXX\zs\)*
(replace XXXXXX with the search word). This will search for everything that does not contain XXXXXX. I imagine if you did:
(用搜索词替换XXXXXX)。这将搜索不包含XXXXXX的所有内容。我想如果你这样做了:
/abc \%(\%(.\{-}\)\@<=defg\zs\)*
This may work like you want it to. Hope this helps a little!
这可能是你想要的。希望这能有所帮助!
#4
-4
/abc\ [^d][^e][^f][^g]
e / abc \[^ d][^][^ f][^ g]
It's pretty cumbersome for larger words, but works like a charm.
对于大一点的词来说,这是相当麻烦的,但它的作用就像一种魅力。