Skip to content

Commit 599fefd

Browse files
authored
Added an explanation for the regex.
Those are always nice, regex code is a mess.
1 parent 307cadf commit 599fefd

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,16 @@ 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+
*/
12371247
Pattern pattern = Pattern.compile("(.*)\\(([^(]+?)\\)([0-9]*)(.*)");
12381248

12391249
while (formula.contains("(")) {

0 commit comments

Comments
 (0)