Saving variable from path with regex processing

Hello everyone.
I am trying to make redirect with custom Host header depending on request path. For example request to http://myapp.local/test1/something must be redirected to http://test1.anotherapp.local/something.

I can save whole path to variable but I can’t find method to trim it to get only test1 part.
Is it even possible now?