I'm having a hard time writing regexes in vim -- it seems everything I try either requires an escape character or isn't supported. For example, I have a SQL statement that includes something like:
max(if(s.name like 'iTunes' and completion_date is not null, 'Complete', if(s.name like 'iTunes', 'X', null))) iTunes,
max(if(s.name like 'amazon' and completion_date is not null, 'Complete', if(s.name like 'Amazon', 'X', null))) Amazon
And I want to change it from 'itunes'
to '%%itunes%%'
for the like statement to work. My first attempt was:
- shiftv to highlight the lines I want to replace
s/'([a-z]+)'/'%%\1%%'
To which I get:
E486: Pattern not found: '([a-z]+)'
How would I enter in that regex, and is there a good place to learn about regex use in vim (specifically what's supported and what needs to be escaped)?
Update: even though it's a bit dry, the docs are great here for a comprehensive reference: pattern
. And the regex that worked was:
s/'\(\a\{2,}\)'/'%%\1%%'/g
A few notes:
- There is no
[a-z]
. Need to use\l
(lowercase) or or\a
(lower or uppercase) instead. - Capturing groups need to be escaped on both sides:
\(...\)
and then reference it with\1
. - For a repetition, the first part needs to be escaped:
\{2,}
Basically, just keep the reference handy as there are a lot of real quirky things here!