I know I could have the user use a common IFS like space, comma, etc., but I would like to be able to have the user simply input their chemical formula (i.e., H2O) and split this into elements and their numbers. For example, if Na2V2O5 is their input, I want to be able to store this information as follows:
Element1: Na Numberofatoms: 2
Element2: V Numberofatoms: 2
Element3: O Numberofatoms: 5
Is it possible to separate things by number or perhaps even looking for capital letters? This is for a bash script.
Yes.
$ IFS=1234567890 read -a atoms <<< "Na2V2O5"
$ declare -p atoms
declare -a atoms=([0]="Na" [1]="V" [2]="O")
However, the numbers themselves are not retained; field splitters are simply discarded.
A word of advice: don't try to write parsers in shell.