If you want to employ all regular expressions constructs and features you can't make it with a simple shell command or a tool, but you need to use a programming language. I use Perl, because it has the best and longest established support for regular expression.
Parenthesized Subexpressions
Earlier, we saw that regex engines support metacharacters \1, \2, \3, etc. to refer
to the text matched by parenthesized subexpressions.
Perl and most other modern regex-endowed languages also provide a way to refer to the text matched
by parenthesized subexpressions from code outside of the regular expression, after a match has been successfully
completed.