I want to split the values based on the number of occurrences.
If the number of occurrences is 4
e.g. key =A-B-C-D
, the answer should beA,B,C,D
If the number of occurrences is more than 4
e.g. key =A-B-C-D-E-F
, the answer should beA-B-C,D,E,F
Please find my attempt below:
String key = "A-B-C-D-E-F";
String[] res = key.split("(?<!^[^_]*)_");
System.out.println(Arrays.toString(res));
My output is A-B,C,D,E,F
but my expectation is A-B-C,D,E,F
Similarly the number of occurrences varies based on usage. While splitting, I need to get maximum four values.
Please check and let me know about this.