Reputation: 15
This is the string:
String strPro = "(a0(a1)(a2 and so on)...(aN))";
Content in a son-() may be "", or just the value of strPro, or like that pattern, recursively, so a sub-() is a sub-tree.
Expected result:
str1 is "a0(a1)"
str2 is "(a2 and so on)"
...
strN is "(aN)"
str1, str2, ..., strN are e.g. elements in an Array
How to split the string?
Upvotes: 1
Views: 401
Reputation: 46209
You can use substring()
to get rid of the outer paranthesis, and then split it using lookbehind and lookahead (?<=
and ?=
):
String strPro = "(a0(a1)(a2 and so on)(aN))";
String[] split = strPro.substring(1, strPro.length() - 1).split("(?<=\\))(?=\\()");
System.out.println(Arrays.toString(split));
This prints
[a0(a1), (a2 and so on), (aN)]
Upvotes: 1