我有一个String来表示一个实体及其属性(也是一个实体),如:
String input = "apple{shape,color},orange{taste,price}";
我想将它拆分为数组:
array[0] = apple{shape,color};
array[1] = orange{taste,price};
任何单词即使它没有{xxx}
也可以被视为一个实体,所以做形状和颜色。
所以,如果我有一个String input = "shape,color"
array[0] = shape
array[1] = color
深度可能更多:String input = "Fruit{apple{shape,color},orange{taste,price}}";
array[0] = Fruit{apple{shape,color},orange{taste,price}}
我试过这个,但效果不好,
public static String[] split(String input){
String[] groups = input.split("(?<=\\}),(?=\\w+\\{)");
return groups;
}
答案 0 :(得分:0)
您可以使用堆栈来检查表达式的平衡,然后将其指定为数组元素。我会为你写一个伪代码:
string s = ""; //initial assignment
stack t;
for i from 0 to exp.length //exp is your input expression
if(exp[i] == ',' && stack is empty && s is not null)
a[k++]=s;
s = "";
continue;
s+=exp[i];
if(exp[i] == '{') //I'm checking balance for '{' only, you can extend it further as required
t.push('{');
if(exp[i] == '}')
t.pop();