mythereal
mythereal

Reputation: 803

Vim regex substitution pattern not found

I have a regex substitution I would like to perform on Vim and the command I have is as follows:

:%s/\s\s(\w)(.*):\s(.*,)/const get\U$1\L$2 = state => $3/g

Problem is, it says the pattern is not found.

Here I have it working on regex101:

https://regex101.com/r/dOmgJy/1

Upvotes: 2

Views: 1708

Answers (2)

clscott
clscott

Reputation: 123

Vim regexp has a concept it calls "magic". You can start your regexp with specific sequence and it changes how that regexp is interpreted.

Most people will find the "very magic" setting (\v) the most familiar when coming from PCRE:

:%s/\v\s\s(\w)(.*):\s(.*,)/const get\U\1\L\2 = state => \3/g

Aside from correcting the capture references ($1 -> \1) the only other change from your original was to prepend the \v sequence to pattern.

Vim's official docs on regexp magic

Upvotes: 1

mangupt
mangupt

Reputation: 389

correct regex for vim will be

:%s/\s\s\(\w\)\(.*\):\s\(.*,\)/const get\U$1\L$2 = state => $3/g

you need to escape the parenthesis.

Upvotes: 4

Related Questions