""""Common pyparsing grammar patterns."""frompyparsingimportalphas,numsfrompyparsingimportGroup,OneOrMore,Optional,Regex,Suppress,Wordimportrepos_neg_int_number=Word('+-'+nums).setParseAction(lambdat:[int(t[0])])# '+3' or '-2' are examples# matching float w/ regex is ugly but is recommended by pyparsingregex_after_decimal=r'([0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)'float_number=Regex(r'[-+]?([0-9]+\.(?!([0-9]|[eE])))|{0}'.format(regex_after_decimal)) \
.setParseAction(lambdat:[float(t[0])])chemical_formula=Group(OneOrMore(Word(alphas,min=1,max=2)+Optional(float_number,default=1.0)))+ \
Optional(Suppress('/')+pos_neg_int_number,default=0)reg_symbol=r'([A-z][A-z]?)'reg_amount=r'([-+]?([0-9]+\.(?!([0-9]|[eE])))|([0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?))?'reg_charge=r'/([+-]?[0-9]+)'chem_regex=reg_symbol+reg_amount