I am trying to write a regular expression in vi to match any whitespace character followed by any digit. Then, at each match, insert a dollar sign between the whitespace and the digit. Here is an example:
A1234 12 14 B1234
B1256 A2 14 C1245
C1234 34 D1 1234K
The correct regex would produce this:
A1234 $12 $14 B1234
B1256 A2 14 C1245
C1234 $34 D1 $1234K
I realize I need to use a back reference, but I can’t quite seem to write the correct regex. Here is my attempt:
:'<,'>/(\s\d)/\s\1\$/g
Also, I have Vim’s default regex mode turned off (vnoremap / /\v).
Thanks for the help.
You need to escape the parentheses to make them work as groupings rather than as actual matches in the text, and not escape the $. Like so:
This worked for me in vim (using standard magic setting).
Edit: just realized that your non-standard regex settings cause you having the escape ‘the other way around’. But still, the trick, I think, is to use two groups. With your settings, this should work: