Q:

Adding prefix and postfix to matching regular expression

Dear fellow Developers,
I would like to quote variables having @ inside like test@mail.org, but not starting with @ like @var1, @var2.
Does Vi/Vim/Neovim have any option to inspect the matched pattern and create a pre- and postfix to test@mail.org: "test@mail.org" ?
:%s/ [a-zA-Z0-9]+@[a-zA-Z0-9]* / \"*\" /
does not work due to \"*\".
If Vi/Vim/Neovim has no such feature, what tools would you recommend for portability?
I know C++ has a feature to inspect the matched regex as a string for manipulation, but I would like to have a more pluggable solution for vim.

A:

Try using \0 to insert the contents of the match in the substitution:
:%s/\<[a-zA-Z0-9.]\+@[a-zA-Z0-9.]*\>/"\0"/g

Note that, given that you gave an example of an email address, I added . to your character classes so that the expression will match these, but it won't only match email addresses. e.g. It would match a@a.
I also changed the white spaces into start/end word atoms: \<, \> so that it will successfully match email addresses abutting punctuation, and added a g flag so that more than one match can be found on each line.

