This can be done with either regex+substitute or macros
Substitute. This is the same as your regex except the important parts are surrounded by \( . \) to create capture groups. These are referred to by submatch(1) and submatch(2) respectively. We use the replace expression \= and execute('let') idiom. Finally, use /n to prevent substitution from ...
Let's get this confusion out of the way first:
Are these ex commands or Vimscript?
Vim scripts are made of ex commands. From :help script:
Your first experience with Vim scripts is the vimrc file. Vim reads it when
it starts up and executes the commands. You can set options to values you
prefer. And you can use any colon command in it (commands that ...
However, I would highly recommend seeing if there is a way to accomplish what you want via simpler (Ex) commands. If you need to do some work over a range, Ex commands are good at it.
For example, visually select the lines (e.g., Vap), then :'<,'>normal! Isome text and :'<,'>normal! Asome text. Then you should be able to ...