I have a regexp that looks like this (simplified a bit):

     pattern = /
     (?<lq>
       %7B                        # single left brace, followed by
       %(?:25|(?![A-Fa-f0-9]{2})) # plaintext '%' or encoded as %25
     )
     |                            # or
     (?<rq>
       %(?:25|(?![A-Fa-f0-9]{2})) # plaintext '%' or encoded as %25
       %7D                        # followed by right brace
     )
     /x

(I know this looks similar to CGI.unescape, but there are special cases.)

I want to use this to replace the captured values in a string. Using 
gsub with a hash seems like the closest thing, but I actually want to 
replace based on the capture group names, not the captured values.

     string = "%7B% test %%7D"
     replacements = {'\k<lq>' => '{{', '\k<rq>' => '}}'}
     expected = "{% string %}"

     string.gsub(pattern, replacements)

Is it possible?

Andrew