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: You may find it easiest to think of the formulae as objects: each chemical element is a tiny hash-based object, so parsing 'H' would return something along the lines of bless { 'H' => 1 }, $class. You can then think of CuSO4 literally as Cu + S + 4*O, and use overloaded add and multiply method calls on the objects. My code does something gnarly to generate a sort of assembler for the world's slowest virtual machine: I wouldn't recommend cutting-and-pasting it! Calculating the RMM is then a simple matter of walking through the object's innards with a while (my ($elem, $count) = each %$self ) loop and using a %rmm hash of $element => $rmm pairs. Hope this helps.

In reply to Re: Regular Expressions and atomic weights by polypompholyx
in thread Regular Expressions and atomic weights by hokie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.