I'm having trouble crafting the appropriate find/replace regex to use in Vim.
Here's some SQL:
select avg(field_name1), avg(field_name2), avg(field_name3) from a_table
Oops, I forgot to alias the results:
select avg(field_name1) field_name1,
avg(field_name2) field_name2, avg(field_name3) field_name3 from a_table
So, in Vim, I'm sure there's a handy find and replace to match an arbitrary [a-z]\+_[a-z]\+
inside parenthesis, but I can't figure out how to put that token after the entire match
Try the following:
:s/(\(.*\))/(\1) \1/g
Group the text that you want to use in the substitution/replacement with surrounding \(\)
, and refer to these back-references in the substitution using \1
, \2
, etc.
Using [:alnum:]
to match alphanumeric characters:
:s/(\([[:alnum:]]\+_[[:alnum:]]\+\))/(\1) \1/g
To apply either regex to the entire file, use:
:%s/(\([[:alnum:]]\+_[[:alnum:]]\+\))/(\1) \1/g
See also :help \1
for more details about using back-references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With