您可以尝试这种方法:
String formula = "C3H20IO";//insert "1" in atom-atom boundry formula = formula.replaceAll("(?<=[A-Z])(?=[A-Z])|(?<=[a-z])(?=[A-Z])|(?<=\D)$", "1");//split at letter-digit or digit-letter boundryString regex = "(?<=\D)(?=\d)|(?<=\d)(?=\D)";String[] atoms = formula.split(regex);
输出:
原子:[C,3,H,20,I,1,O,1]
现在所有偶数索引(0、2、4 …)都是原子,奇数是关联的数字:
String[] a = new String[ atoms.length/2 ];int[] n = new int[ atoms.length/2 ];for(int i = 0 ; i < a.length ; i++) { a[i] = atoms[i*2]; n[i] = Integer.parseInt(atoms[i*2+1]);}
输出:
a:[C,H,I,O]
n:[3、20、1、1]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)