'Recursively extract contents of (nested) parentheses in string, replace selected content(s) down to a single (char+int), read again and repeat

This is my first post so please comment down if you need further clarification, Say we take in a string such as:

((((a).(5)).((a)*)).((b)*))*

and through the process, we perhaps count++ the amount of '(' read and count-- the amount of ')' read until we come across our first char or variable (we can consider other operators such as '.' or '|' or '*') that is the left and innermost content such that a is replaced so that our string now reads:

(((R1.(5)).((a)*)).((b)*))*

We must consider when a is selected, we also include its parenthesis as well but only consider the string that is read (perhaps store it in or as a vector, pointer, object, etc.), the same applies for when 5 which results in:

(((R1.R2).((a)*)).((b)*))*

at this moment we perhaps find now that the innermost content found is string (R1.R2) and this results the string to be converted into R3 and having the string as:

((R3.((a)*)).((b)*))*

We continue the iteration for a to be read too.

((R3.(R4*)).((b)*))*

If a star * is read, we can consider extracting that content with R4 to be replaced as R5

((R3.R5).((b)*))*

Our next iteration follows:

((R3.R5).(R6*))*

((R3.R5).R7)*

(R8.R7)*

R9*

and finally as our final result,

R10

And we end.

I've already tried a variety of algorithms from several sources for hours yet I'm still stuck and puzzled at the same spot and I may not be thinking this through very properly as I had hoped.

The only closest that was modified was this:

https://www.geeksforgeeks.org/extract-substrings-between-any-pair-of-delimiters/

but I'm still puzzled about what must be properly implemented.

How would you make this possible? Any solutions or a straight post of your code could surely help me understand this process.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source