title: Parsing equations in Python using regex
body: <p>I have the following expressions that I need to be able to parse into separate tokens:</p>
  
  <pre><code>"a=b+c"     -&gt;  a b + c
  "a=5+c"     -&gt;  a 5 + c
  "a=c*50"    -&gt;  a c * 50
  "a=50%50"   -&gt;  a 50 % 50
  "a=c-x[20]" -&gt;  a c - x[20]
  "a=x[3]+50" -&gt;  a x[3] + 50
  "a=b--c"    -&gt;  a b - -c
  </code></pre>
  
  <p>I used <a href="https://txt2re.com/%20this%20regex%20help" rel="nofollow noreferrer">this regex help</a> but I can only apply the regex on one example, not all of them. For example, the following code would work only on "a=b+c","a=c-50" but not if I swap them into "a=c-50, "a=b+c":</p>
  
  <pre><code>#!/usr/bin/python
  # URL that generated this code:
  # http://txt2re.com/index-python.php3?s=%22a=b%2bc%22,%20%22a=c-50%22&amp;-32&amp;-34&amp;25&amp;-35&amp;-33&amp;36&amp;27&amp;5
  
  import re
  
  txt='"a=b+c", "a=c-50"'
  
  re1='.*?'   # Non-greedy match on filler
  re2='(a)'   # Any Single Character 1
  re3='.*?'   # Non-greedy match on filler
  re4='(b)'   # Any Single Character 2
  re5='(.)'   # Any Single Character 3
  re6='(c)'   # Any Single Character 4
  re7='.*?'   # Non-greedy match on filler
  re8='(a)'   # Any Single Character 5
  re9='.*?'   # Non-greedy match on filler
  re10='.'    # Uninteresting: c
  re11='.*?'  # Non-greedy match on filler
  re12='(.)'  # Any Single Character 6
  re13='(.)'  # Any Single Character 7
  re14='(\\d+)'   # Integer Number 1
  
  rg = re.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12+re13+re14,re.IGNORECASE|re.DOTALL)
  m = rg.search(txt)
  if m:
     c1=m.group(1)
     c2=m.group(2)
     c3=m.group(3)
     c4=m.group(4)
     c5=m.group(5)
     c6=m.group(6)
     c7=m.group(7)
     int1=m.group(8)
     print("("+c1+")"+"("+c2+")"+"("+c3+")"+"("+c4+")"+"("+c5+")"+"("+c6+")"+"("+c7+")"+"("+int1+")"+"\n")
  </code></pre>