Skip to content

Commit 545c261

Browse files
authored
Updated with a non regex aproach
1 parent 599fefd commit 545c261

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

tool/formula/src/main/java/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator.java

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,35 +1234,42 @@ public static String simplifyMolecularFormula(String formula) {
12341234
* @return Formula with the correction
12351235
*/
12361236
private static String breakExtractor(String formula) {
1237-
/* Regex explanation:
1238-
* (.*) Takes everything from before the formula (Group 1)
1239-
* \( Matches the open bracket "(" (No group)
1240-
* ([^(]+?) Matches everything that isn't an opening
1241-
* bracket (The formula inside the inner most bracket) (Group 2)
1242-
* \) Matches a closing bracket (No group)
1243-
* ([0-9]*) Matches the multiplier number, if any (Group 3)
1244-
* (.*) Takes everything from after the formula to remove the
1245-
* parenthesis from (Group 4)
1246-
*/
1247-
Pattern pattern = Pattern.compile("(.*)\\(([^(]+?)\\)([0-9]*)(.*)");
1248-
1249-
while (formula.contains("(")) {
1250-
Matcher matcher = pattern.matcher(formula);
1251-
String newFormula = formula;
1252-
1253-
while ( matcher.find() ) {
1254-
String multiplierStr = matcher.group(3);
1255-
int multiplier = multiplierStr.isEmpty() ? 1:Integer.parseInt(multiplierStr);
1256-
newFormula = matcher.group(1) + muliplier(matcher.group(2), multiplier) + matcher.group(4);
1237+
boolean finalBreak = false;
1238+
1239+
int innerMostBracket = formula.lastIndexOf("(");
1240+
1241+
if (innerMostBracket<0)
1242+
return formula;
1243+
1244+
String finalformula = formula.substring(0, innerMostBracket);
1245+
String multipliedformula = "";
1246+
String formulaEnd = "";
1247+
String multiple = "";
1248+
1249+
for (int f = innerMostBracket + 1; f < formula.length(); f++) {
1250+
char thisChar = formula.charAt(f);
1251+
1252+
if ( finalBreak ) {
1253+
if ( isDigit(thisChar) ){
1254+
multiple += thisChar;
1255+
} else {
1256+
formulaEnd = formula.substring(f, formula.length());
1257+
break;
1258+
}
1259+
}else {
1260+
if ( thisChar == ')' ) {
1261+
finalBreak = true;
1262+
}else
1263+
multipliedformula += thisChar;
12571264
}
1258-
1259-
if (newFormula == formula)
1260-
return formula;
1261-
formula = newFormula;
1262-
}
1263-
1264-
return formula;
1265-
}
1265+
}
1266+
finalformula += muliplier(multipliedformula, multiple.isEmpty() ? 1:Integer.valueOf(multiple)) + formulaEnd;
1267+
1268+
if (finalformula.contains("("))
1269+
return breakExtractor(finalformula);
1270+
else
1271+
return finalformula;
1272+
}
12661273

12671274
/**
12681275
* The starting with numeric value is used to show a quantity by which a formula is multiplied.

0 commit comments

Comments
 (0)