如何在Java中拆分期间忽略所需的字符串?

时间:2018-02-06 10:40:54

标签: java regex string

我有一个像

这样的字符串
pchase_history:array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>,first_pchase_dt:string,last_pchase_dt:string,trans_cnt:bigint,last_pchase_sku_cnt:bigint,no_of_pchase_days:bigint,lst_pchase_channel:array<struct<pchase_channel:string>>

我需要将其拆分为&#39;,&#39;但不想分裂(结构数组)array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>
我希望split方法忽略这些struct数组并拆分其余的字符串。

我如何通过拆分方法实现这一目标?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式替换之前的struct 数组,如下所示进行拆分:

String value = "pchase_history:array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>,first_pchase_dt:string,last_pchase_dt:string,trans_cnt:bigint,last_pchase_sku_cnt:bigint,no_of_pchase_days:bigint,lst_pchase_channel:array<struct<pchase_channel:string>>";

value = value.replaceAll("(array<struct<.*?>>)", "array");
String[] splitedValues = value.split(",");

System.out.println(Arrays.toString(splitedValues));

输出:

[pchase_history:array, first_pchase_dt:string, last_pchase_dt:string, trans_cnt:bigint, last_pchase_sku_cnt:bigint, no_of_pchase_days:bigint, lst_pchase_channel:array]

Click here to test regex online