parse numbers from x,y,z equation
<p>I am parsing numbers from equation. From my code there is little problem. 
It does not recognize the number 1 from equation because normally in equation, number 1 is skipped. </p>

<pre><code>def equationSystem(e):
a = []
for s in e:
    a.append(re.findall("[-]?\d+[\.]?\d*[eE]?[-+]?\d*", s))

print a[0]
</code></pre>

<p>example </p>

<pre><code>equation =  ["-x+y+z=0", "x-3y-2z=5", "5x+y+4z=3"]
</code></pre>

<p>expected output</p>

<pre><code>[[-1, 1, 1, 0], [1, -1, -2, 5], [5, 1, 4, 3]]
</code></pre>

<p>but actual output is </p>

<pre><code>[[0], [-2, 5], [5,1,4,3]]
</code></pre>

<p>can you help me to improve the regular expression?</p>

<p>This should work fine:</p>

<pre><code>pat = re.compile(r"(?:(?&lt;=^)|(?&lt;=[+-]))[a-z]")
</code></pre>

<p>Here the pattern <code>pat</code> will help substitute all the Non-Digit-Preceding characters by <code>1</code>, for eg:<br>
<code>-x+y+z=0</code> becomes <code>-1+1+1=0</code> and <code>5x+y+4z=3</code> becomes <code>5x+1+4z=3</code></p>

<pre><code>for x in equation:
  s = re.sub(pat, "1", x) # substitute by "1"
  print (re.findall(r"[-]?\d", s)) # find digits (with signs)
</code></pre>

<p>this gives:</p>

<pre><code>['-1', '1', '1', '0']
['1', '-3', '-2', '5']
['5', '1', '4', '3']
</code></pre>

python