-3

I have a string like

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>>

and i need to split it by ',' but don't want to split (array of struct) array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>
I want split method to ignore these array of struct and split the rest of the string.

How can i achieve this by split method?

Any help would be appreciated.

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
  • 3
    What have you [tried](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)? Where are you stuck at? – user202729 Feb 06 '18 at 10:42
  • Possible duplicate of [Regex to match only commas not in parentheses?](https://stackoverflow.com/questions/9030036/regex-to-match-only-commas-not-in-parentheses) – Sebastian Simon Feb 06 '18 at 10:44
  • i am not able to do it using split method.I tried by iterating every column then checking for array of struct. – Vishwanath Sharma Feb 06 '18 at 10:44
  • Then use something else. – user202729 Feb 06 '18 at 10:44
  • [Regex can't match everything](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). – user202729 Feb 06 '18 at 10:45

1 Answers1

0

You can use a regex to replace your array of struct before doing split like this:

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));

Output:

[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

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27