RegEx for parsing chemical formulas

后端 未结 4 1510
生来不讨喜
生来不讨喜 2020-12-03 16:05

I need a way to separate a chemical formula into its components. The result should look like this:

   Ag3PO4 -> [         


        
4条回答
  •  悲哀的现实
    2020-12-03 16:47

    When you encounter a parenthesis group, you don't want to parse what's inside, right?

    If there are no nested parenthesis groups you can simply use

    [A-Z][a-z]*\d*|\([^)]+\)\d*
    

    \d is a shorcut for [0-9], [^)] means anything but a parenthesis.

    See demo here.

提交回复
热议问题