I'm working on one requirement. I receives input string in below format.
A:82% X 18% Y B:100% X C:82% X 18% Y AB:60% X 20% Y 20% ZZ
String Explanation,
1) String consists of multiple material names like below are the material names present in above mentioned string
A
B
C
AB
2) Every material is made up of different constituents, For example A is made from 82% of X and 18% of Y. In another input string according to material name the ratio of ingredients can split accordingly. But total is always 100%
3) A string can have multiple material names and one material can be made of n number of ingredients (Total percentage would be 100%)
I want to convert my input string in below format
#A:82% X,18% Y #B:100% X #C:82% X, 18% Y #AB:60% X,20% Y,20% ZZ
I'm able to achieve hash part using regex, code snippet
String inp = "A:82% X 18% Y B:100% X C:82% X 18% Y AB:82% X 18% Y";
String regex = "(\\b[A-Za-z]{1,}\\:\\b)";
System.out.println(inp.replaceAll(regex, "#$1"));
But am not able to handle or not getting idea for setting up commas in between ingredients of specific material.
Any suggestions please....?