I'm trying to learn about Recursion in Regular Expressions, and have a basic understanding of the concepts in the PCRE flavour. I want to break a string:
Geese (Flock) Dogs (Pack)
into:
Full Match: Geese (Flock) Dogs (Pack)
Group 1: Geese (Flock)
Group 2: Geese
Group 3: (Flock)
Group 4: Dogs (Pack)
Group 5: Dogs
Group 6: (Pack)
I know neither regex quite does this, but I was more curious as to the reason why the the first pattern works, but the second one doesn't.
Pattern 1: ((.*?)(\(\w{1,}\)))((.*?)(\g<3>))*
Pattern 2: ((.*?)(\(\w{1,}\)))((\g<2>)(\g<3>))*
Also, if for example you're dealing with a long string, and a pattern repeats itself, is it possible to continually expand the full match, and incrementally increase the groups without writing a loop statement separate to the regex.
Full Match: Geese (Flock) Dogs (Pack) Elephants (Herd)
Group 1: Geese (Flock)
Group 2: Geese
Group 3: (Flock)
Group 4: Dogs (Pack)
Group 5: Dogs
Group 6: (Pack)
Group 7: Elephants (Herd)
Group 8: Elephants
Group 9: (Herd)
This is the closest I've came to was this pattern, but the middle group: Dogs (Pack) becomes Group 0.
((.*?)(\(\w{1,}\)))((.*?)(\g<3>))*