Can we use named references on the RHS in StringReplace?

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 named n which matches ...
  • (?P=n) is a back-reference to the latest match for the pattern named n
  • (?P>n) is a reference to the pattern named n 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"] 

Is it possible to reference capturing group by its name on the RHS of a replacement rule?


I think we are out of luck. I cannot offer a definitive answer to this question, but I will share some observations.

It is only with the release of PCRE2 in 2015 that substitution replacement string syntax became standardized through the function pcre2_substitute. The original PCRE library did not offer substitution functionality so it was left up to client code to implement such functionality itself.

Mathematica is certainly not using pcre2_substitute because it does not interpret the escape sequence $$ as a single dollar sign character:

StringReplace["x", RegularExpression["(.)"] :> "$1 $$ $1"]

(* "x $$ x" *)

Since PCRE2 is not backwards-compatible with original PCRE, Mathematica might continue to use the original library for a long time.

Therefore, we must presume that Mathematica is implementing its own substitution string syntax. This means that, unlike regex pattern syntax, we cannot use the PCRE documentation to determine what is valid. We can only rely on the Mathematica documentation itself. And that documentation says that only numeric back-references are supported. (Named references would be a nice feature request though -- especially since numbered references are not always reliable as shown in the linked question.)

Category: string manipulation Time: 2016-07-31 Views: 2

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.199 (s). 12 q(s)