How can I achieve something like this: "Ca(OH)2" => "Ca" and "(OH)2"
In python, it can be achieved like this:
import re
compound = "Ca(OH)2"
segments=re.split('(\([A-Za-z0-9]*\)[0-9]*)',compound)
print(segments)
Output: ['Ca', '(OH)2', '']
I am following this tutorial from https://medium.com/swlh/balancing-chemical-equations-with-python-837518c9075b (except that I wanted to do it in Java)
(\([A-Za-z0-9]*\)[0-9]*)To breakdown the regex, the outermost parenthesis(near the single quotes) indicate that that is our capture group and it is what we want to keep. The inner parenthesis with the forward slash before them mean that we want to literally find parenthesis(this is called escaping) the [A-Za-z0–9] indicate that we are ok with any letter(of any case) or number within our parentheses and the asterisk after the square brackets is a quantifier. It means that we are ok with having zero or infinite many letters(of any case) or numbers within our parenthesis. and the [0–9] near the end, indicate that we want to include ALL digits to the right of our parenthesis in our split.
I tried to do it in Java but the output was not what I wanted:
String compound = "Ca(OH)2";
String[] segments = compound.split("(\\([A-Za-z0-9]*\\)[0-9]*)");
System.out.println(Arrays.toString(segments));
Output: [Ca]