We can use group numbers to reuse the same piece of code while inside regex string, like that:

    StringCases["x = y", RegularExpression["([\\w\\s]+)=(?1)"]]

> `{"x = y"}`

To make it more readable and robust (like avoiding the situation when the groups can be renumbered either [internally][1] or through a redesign of the regex code), we can name patterns (and yes, `(?<n>) / (?&n)` syntax works as well as `(?P<n>) / (?P>n)`):

    StringCases["x = y", RegularExpression["(?<var>[\\w\\s]+)=(?&var)"]]
    StringCases["x = y", RegularExpression["(?P<var>[\\w\\s]+)=(?P>var)"]]

> `{"x = y"}`

> `{"x = y"}`

Perfect so far. However, what if I want to use the groups in the replacement rule? This works:

    StringCases["x = y", RegularExpression["([\\w\\s]+)=((?1))"] :> {"$1", "$2"}]

> `{{"x ", " y"}}`

But this doesn't:

    StringCases["x = y", RegularExpression["(?<var>[\\w\\s]+)=(?<rhs>(?&var))"] :> {"${var}", "${rhs}"}]

> `{{"${var}", "${rhs}"}}`

Of course, I can still use the numbers to refer to the groups:

    StringCases["x = y", RegularExpression["(?<var>[\\w\\s]+)=(?<rhs>(?&var))"] :> {"$1", "$2"}]

> `{{"x ", " y"}}`

But this is exactly what I want to avoid. I want to use the names, as they won't be affected by the code redesign.

I also tried `$+{name}`, `\g<name>`, but they all don't work. 

So, the question is: can we use names of the groups in the replacement rule?


  [1]: http://mathematica.stackexchange.com/a/72739/44141