How to split this string in Java regex? -
this string:
string strpro = "(a0(a1)(a2 , on)...(an))";
content in son-() may "", or value of strpro, or pattern, recursively, sub-() sub-tree.
expected result:
str1 "a0(a1)" str2 "(a2 , on)" ... strn "(an)"
str1, str2, ..., strn e.g. elements in array
how split string?
you can use substring()
rid of outer paranthesis, , split using lookbehind , lookahead (?<=
, ?=
):
string strpro = "(a0(a1)(a2 , on)(an))"; string[] split = strpro.substring(1, strpro.length() - 1).split("(?<=\\))(?=\\()"); system.out.println(arrays.tostring(split));
this prints
[a0(a1), (a2 , on), (an)]
Comments
Post a Comment