Vim regex grouping

Posted on Tue 28 July 2020 in dev-journal

Wrote a regex in vim today that used a grouping in the find and replace. Needed to update 936 instances..

From a statement that calls a custom module function {% if entry.getEnableVal('conveyorDevToggle') == 1 to be a simple statement.. {% if entry.conveyorDevToggle %}

This is the regex used: :%s/\ventry.getEnableVal\('(\w+)'\) \=\= 1/entry.\1/gc

The key notes are:


:help \v

Use of "\v" means that after it, all ASCII characters except '0'-'9', 'a'-'z',  
'A'-'Z' and '_' have special meaning: "very magic"                              

Use of "\V" means that after it, only a backslash and terminating character     
(usually / or ?) have special meaning: "very nomagic"         


where \1 holds the first word, \2 - any number of spaces or tabs in between and \3 - the second word. How to decide what number holds what pair of \(\) ? - count opening "\(" from the left.

Using the above documentation the regex was constructed with the (\w+) grouping being used with the \1 group

:%s/\ventry.getEnableVal\('(\w+)'\) \=\= 1/entry.\1/gc