jjk
jjk

Reputation: 595

Search string enclosed in quotes in Vim

In vim I need to search all strings in quotes e.g. 'foo'

Does one see the problem in this regex? E486: Pattern not found \'([^']*)'

:\/'([^']*)'

Regex Tester

Upvotes: 2

Views: 2722

Answers (2)

builder-7000
builder-7000

Reputation: 7657

Alternatively you can use

\v'(\a+)'

this regex performs similar than yours, except when nested quotes are encountered. In the text:

The user's first 'answer'.

The regex \v'(\a+)' will capture answer while your original regex (corrected by sidyll) \v'([^']*)' will capture 's first '.

Upvotes: 2

sidyll
sidyll

Reputation: 59307

First problem is that your use of find is a bit confusing. If you want to just find, use /. The colon is not necessary (which indicates command mode). If you're using the find as a range (basically the same thing, / is just an empty command with a range) you can use the colon, but either way escaping the first slash is not necessary.

The other main problem is that parenthesis by default need to be escaped if you meant a capturing group. All of this is dependant on your 'magic' option reading the help for the /magic topic (you can do a :h magic) is highly recommended. With "vanilla" Vim settings, the regex you need looks live this:

/'\([^']*\)'

With very magic enable (by using the \v atom) this can be simplified to your original design:

/\v'([^']*)'

Upvotes: 3

Related Questions