in reply to Regular Expressions and atomic weights
One way is to convert the string into Perl code:
my %atom_weights = ( Pb => ..., C => ..., O => ..., ... ); $_ = "Pb(CO3)2"; print("$_\n"); s/([0-9]+)/*$1/g; s/([A-Z][a-z]*)/ ($atom_weights{$1} or die("Bad element $1\n") ) . '+' /eg; s/\+(?=\*|$)//g; print("$_\n"); print(eval($_), "\n");
Of course, using eval is dangerous unless you validate your input.
Update: Fixed code. * => +, ** => *
|
|---|