Parsing a mathematical string expression in Python using regular expressions
<p>I've built an expression evaluator using an AST and would now like to connect it to a parser. I want to build out the parsing on my own using regular expressions, without relying on tools like Pyparser, or the tokenize library.</p>

<p>Valid symbols: real numbers, +, -, *, /, (, )
Number format: any valid real number with or without a decimal, with an optional positive/negative sign prefixed to it.</p>

<pre><code>    num_pattern = r"[-+]?[0-9]*\.?[0-9]+"
    symbol_pattern = "[/+-/*()]"
</code></pre>

<p>Input is an expression containing a number or symbol, followed by <strong>optional</strong> white space, followed by a number or symbol and repeat.</p>

<p>I want to find each token, strip any whitespace, add a single space, and reassemble the output (which I will then turn into a white space separated list). </p>

<p>My two biggest problems are that the negative sign (-) can appear both as part of a number and as an operator. I'm not sure how to go about separating the two cases. I'm also having difficulty grouping the results together in a way that will let me replace them. I can find all of the numbers in the string, and I can find all of the symbols, but I don't know how to create a single regEx that will break each multi-digit number and each symbol into a group which I can this replace/add a space to. I know this has something to do with using a greedy match, but I'm lost as to how.</p>

<p>Examples:</p>

<pre><code>(-23 + 8) * 5   --&gt;   ( -23 + 8 ) * 5
-23--23         --&gt;   -23 - -23
.3 * (-3+ 9)    --&gt;   .3 * ( -3 + 9 )
</code></pre>