user697911
user697911

Reputation: 10531

How to search this pattern in vim?

/'text': '.+'

In VIM, I want to match up to 'abc' and 'a' from 'text' in two cases:

'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': '[^']\+'

Upvotes: 2

Views: 73

Answers (1)

DJMcMayhem
DJMcMayhem

Reputation: 7669

The reason this isn't working is because the + atom must be escaped to have a special meaning in vim's flavor of regex. 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,}. Try this:

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

Of course, you could also turn on the magic option and do this:

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

A nice overview on vim regex can be found at vimregex.com. I use it all the time.

Upvotes: 6

Related Questions