Chemical Forums

Specialty Chemistry Forums => Other Sciences Question Forum => Topic started by: ajri02 on June 08, 2016, 10:21:34 AM

Title: Parsing a chemical formula
Post by: ajri02 on June 08, 2016, 10:21:34 AM
I'm trying to create a java app that takes a chemical formula like "CH3COOH" and returns some sort of collection full of their symbols.

CH3COOH would return [C,H,H,H,C,O,O,H]

I already have something that is kinda working, but it's very complicated and uses a lot of code with a lot of nested if-else structures and loops.

Is there a way I can do this by using some kind of regular expression with String.split or maybe in some other brilliant simple code?
Title: Re: Parsing a chemical formula
Post by: AWK on June 08, 2016, 10:56:33 AM
Try using SMILES on the forum

http://www.chemicalforums.com/index.php?topic=59314.msg230001#msg230001
Title: Re: Parsing a chemical formula
Post by: Borek on June 08, 2016, 11:58:01 AM
Is there a way I can do this by using some kind of regular expression

Long ago I did something similar in php, with a callback function generating replacement strings from the string and the number of repetitions. It worked by replacing each item with a subscript by a list, so that for example (NH4)2SO4 would gradually become
NH4NH4SO4 (removing parentheses first)
NHHHHNH4SO4 (proceeding with indices from the left)
NHHHHNHHHHSO4
NHHHHNHHHHSOOOO

Title: Re: Parsing a chemical formula
Post by: ajri02 on June 08, 2016, 08:13:57 PM
Thank Borek