Thanks @AprioriFish3134 for pointing out the original issue (no \2), and @ngambles for a good solution.
If you need even more levels of nested parens, or the sequential parens in your example, just count the open-parens from left-to-right to discover the back reference number. The back reference for unused alternatives is an empty string.
Regex( "abac", "(((a)|(b)|(c))(c))+", "1=\1 2=\2 3=\3 4=\4 5=\5 6=\6" )
"1=ac 2=a 3=a 4= 5= 6=c"
One thing that might not be obvious: the back reference can also be used in the pattern. This could be useful for making sure a leading and trailing quotation mark or apostrophe are used consistently:
Write( Regex(
"before 'a \!"b\!" c' after", // before 'a "b" c' after
"(\!"|')(.*?)\1", // ("|')(.*?)\1
"\2"
) )
a "b" c
(The \!" escape puts a quotation mark inside the strings; the comment shows the text of the string.) In this example, \1 group can match either " or ' and \2 matches all of the text up to another occurrence of \1.
Use some comments if you write regex like that! The next maintainer will appreciate it.
Craige