简体   繁体   English

如何在vim中搜索此模式?

[英]How to search this pattern in vim?

/'text': '.+'

In VIM, I want to match up to 'abc' and 'a' from 'text' in two cases: 在VIM中,我想在两种情况下匹配“文本”中的“ abc”和“ a”:

'text': 'abc', 'url': 'http...'
'text': 'a', 'title': 'dog'

I want to match at least one character in the single quote after the colon. 我想在冒号后的单引号中匹配至少一个字符。 This doesn't seem to work. 这似乎不起作用。

I got it. 我知道了。 This worked! 这工作了!

/'brand': '[^']\+'

The reason this isn't working is because the + atom must be escaped to have a special meaning in vim's flavor of regex. 之所以不起作用,是因为必须对+原子进行转义,以使其具有vim的正则表达式风格的特殊含义。 Right now this means anything followed by a literal plus . 现在,这意味着紧随其后的是文字加号 But when escaped, it means at least one, but as many as possible . 但是,一旦逃脱,就意味着至少一个,但要尽可能多 So you'd want this: 所以你想要这个:

/'text': '.\+'

However, this creates a new problem. 但是,这产生了新的问题。 The .+ will match as many characters as possible, including the single quotes. .+将匹配尽可能多的字符,包括单引号。 So it ends up matching 所以最终匹配

abc', 'url': 'http...'

Instead of 代替

abc'

So you want to use the non-greedy equivalent of + which is \\{-1,} . 因此,您想使用+非贪婪等价物\\{-1,} Try this: 尝试这个:

/'text': '.\{-1,}'

Of course, you could also turn on the magic option and do this: 当然,您也可以打开magic选项并执行以下操作:

/\v'text': '.{-1,}'

A nice overview on vim regex can be found at vimregex.com . 可以在vimregex.com上找到有关vim regex的完整概述。 I use it all the time. 我用它所有的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM