I am working on converting a parenthesized string such as f(d(a c(b))e)
into a Tree data structure in Java (I am working on a method which would allow one to instantiate a Tree using the string representation). In the above string, f
is the tree's root node which branches off into a subtree at d
and a leaf-node at e
. After I was able to identify f
as the current node's label, I am left with d(a c(b))e
.
I would like to be able to use Java's regular expressions to identify the children; in this case, d(a c(b))
and e
. So, the requirements are as follows.
In the string, a single character may or may not be followed by parenthesis. If it is followed by parenthesis, return all of the substring inside, even if it contains nested parenthesis. So, the regular expression would match d(a c(b))
or e
.
Moreover, I want this to work on more than just nodes with two children. A possible parenthesized string might be f(a b c)
which is a tree rooted at f
with 3 leafs.
So far, I have .\(?[^\(\)]\)?
but this doesn't work.