Skip to main content
Post Closed as "Duplicate" by Alexey Popkov, MarcoB, CommunityBot
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

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 internallyinternally 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?

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 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?

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 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?

Tweeted twitter.com/StackMma/status/852217236613996548
edited tags
Link
Source Link
Stitch
  • 4.2k
  • 1
  • 13
  • 28

Regex named groups -- how to refer back to them in the replacement string

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 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?