in reply to Regular Expressions and atomic weights
I wrote a calculator module that does exactly this for chemical formula strings. It's my pet wheel-reinvention, but the RMM thing has actually been very useful (I'm a biochemistry lecturer). I would post the code, but it's a bit huge: just look in the Chemistry.pm module in the tarball. It's actually an extension to a more general calculator thing, but you'll probably find the Parse::RecDescent grammar useful: as other posters have said, a regex cannot parse general chemical formulae, because they are inherently nested (it's the same reason regexes can't be used to parse HTML in anything but the ugliest hacks).
Some general things to consider are:
- Do you need the grammar to understand complicated things like Fe2(SO4)3.9H2O? If this answer to this is "yes", you need a Parse::RecDescent-style (context-free) grammar: regexes will not work.
- Does it need to understand common shorthands like Et, Me, Ph and Ac?
- Does it need to understand H, T, D and the hideous nomeclatural mess of the transactinides?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Regular Expressions and atomic weights
by ikegami (Patriarch) on Jul 26, 2005 at 00:19 UTC | |
by hokie (Monk) on Jul 26, 2005 at 12:58 UTC |
In Section
Seekers of Perl Wisdom