Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是字符串:
String strPro = "(a0(a1)(a2 and so on)...(aN))";
son-() 中的内容可能是 "",或者只是 strPro 的值,或者类似该模式,递归地,所以 sub-() 是一个子树。
预期结果:
str1 is "a0(a1)" str2 is "(a2 and so on)" ... strN is "(aN)"
str1, str2, ..., strN 是数组中的元素
如何拆分字符串?
您可以使用substring()去掉外部括号,然后使用lookbehind和lookahead(?<=和?=)拆分它:
substring()
?<=
?=
String strPro = "(a0(a1)(a2 and so on)(aN))"; String[] split = strPro.substring(1, strPro.length() - 1).split("(?<=\\))(?=\\()"); System.out.println(Arrays.toString(split));
这打印
[a0(a1), (a2 等), (aN)]