Twig replace filter - use part of regex match
Craft's replace filter will just use PHP's preg_replace method behind the scenes if it detects that a regular expression has been passed in.

It sounds like you're looking for the PHP preg_match method, which isn't available natively.

There was a plugin that added support for this in Craft 2, but I don't think it's been ported to Craft 3. You might be able to use that as a starting point if you'd like to port it, though.