In this great answer WReach demonstrated some advantages of usage of Named Patterns inside of RegularExpression
and described the supported syntax:
PCRE in Mathematica appears to be configured so that only
(?P)
syntax is supported:
(?P<n>...)
defines the pattern namedn
which matches...
(?P=n)
is a back-reference to the latest match for the pattern namedn
(?P>n)
is a reference to the pattern namedn
itself (e.g. for recursion)
It is well-documented that we can reference Numbered Patterns on the RHS via the $n
syntax (where n
is the number of the capturing group). But how can we provide a reference via the name of the capturing group? I have tried it the same way as with Numbered Patterns but it produces strange unexpected results:
StringReplace["1234", RegularExpression["(?P<name>.*)"] :> "$name"]
"$name$name"
Is it possible to reference capturing group by its name on the RHS of a replacement rule?
Update: As Stitch notes in this duplicate question, "the (?<n>)
/(?&n)
syntax works as well as (?P<n>)
/(?P>n)
."