in reply to Re: Re: Regexps for Parsing Brackets in Chemical Formulae
in thread Regexps for Parsing Brackets in Chemical Formulae
It prints:use strict; my %count; # added gratuitous parentheses for embedded formula testing sake. $_='Mo(P(H)3)4(CO)(NH2C2(H)5)'; # at each iteration do subformula with rigtmost left parenthesis. # quit when no more parenthesis s/(.*)\((.*?)\)(\d*)/$1 . $2 x ($3 ? $3 : 1) /e while m/\(/; s/([A-Z](?:[a-z])?)(\d*)/ $count{$1} += $2 ? $2 : 1 ;''/eg; printf "%-2s %3d\n", $_, $count{$_} for sort keys %count;
C 3 H 19 Mo 1 N 1 O 1 P 4
-- stefp
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Regexps for Parsing Brackets in Chemical Formulae
by Elgon (Curate) on Nov 05, 2001 at 09:26 UTC | |
by stefp (Vicar) on Nov 07, 2001 at 22:27 UTC |