title: python regex parsing data from extremely complicated ingredients string
body: <p>I have a string that contains some terribly formatted ingredients list (shortened for this example):</p>
  
  <pre><code>Vitamin A 6,000iu/kg, Vitamin D3 80iu/kg, Vitamin E PLUS 240iu/kg
  </code></pre>
  
  <p>I want to break the string down into a list containing the format <code>[label, amount, units]</code>:</p>
  
  <pre><code>[[Vitamin A, 6000, iu/kg], ...]
  </code></pre>
  
  <p>The problem is there are <code>,</code> in the numbers (ie, <code>6,000</code>) and as a separator. I can't simply split by comma. The labels can be any number of letters/numbers (ie, <code>Super Duper Vitamin c4390 4.5iu/kg</code>), which makes distinguishing between labels and amount even harder. the units can vary from <code>mg/kg</code> and <code>iu/kg</code>. The list is not restricted to <code>Vitamin</code> ingredients. It can contain other words like <code>Potassium</code>. There are decimals as well.</p>
  
  <p>The other problem is that there are edge cases:</p>
  
  <p><strong>Edge cases</strong>: missing space after comma <strong><em>AND</em></strong> some ingredients may be missing a comma separator.</p>
  
  <pre><code>Vitamin A 6,000iu/kg,Vitamin D3 80iu/kg Vitamin E PLUS 240iu/kg, Potassium 3.2mg/kg
  </code></pre>
  
  <p>The best I can do is this regex:</p>
  
  <pre><code>^(([a-zA-Z]*\s+)*(\d+[,]?\d+)([a-z\/]*))
  </code></pre>
  
  <p>Which (not quite) matches the first ingredient, and it doesn't handle any of the edge cases. How can I extract the data I want from this messy string?</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p><a href="http://pythex.org/?regex=%5Cb%28[a-zA-Z][a-zA-Z%20%5Ct]*%29%5Cw%2B%28%5Cd%2B[%2C]%3F%5Cd%2B%29%28[a-z%5C%2F]*%29%2C[%20%5Ct]*&amp;test_string=Vitamin%20A%2015%2C000iu%2Fkg%2CVitamin%20D3%202%2C000iu%2Fkg%2C%20Vitamin%20E%20200iu%2Fkg%2CZinc%20Sulphate%20Monohydrate%20417mg%2Fkg%2C%20Manganous%20Oxide%20131mg%2Fkg%2CFerrous%20Sulphate%20Monohydrate%20297mg%2Fkg%2C%20Calcium%20Iodate%20Anhydrous%207.9mg%2Fkg%2C%20Sodium%20Molybdate%206.4mg%2Fkg%2CCupric%20Sulphate%20Pentahydrate%202.4mg%2Fkg%20Sodium%20Selenite%200.2mg%2Fkg&amp;ignorecase=0&amp;multiline=0&amp;dotall=0&amp;verbose=0" rel="nofollow">Here is a real example</a></p>