Capturing all operators, parentheses and numbers separately in "(3 + 44)* 5 / 7" with Regex
<p>For input string: <code>st = "(3 + 44)* 5 / 7"</code></p>

<p>I'm looking to get the following result using only regex: <code>["(", "3", "+", "44", ")", "*", "5", "/", "7"]</code></p>

<p>Attempts:</p>

<ol>
<li><pre><code>&gt;&gt;&gt; re.findall("[()\d+\-*/].?", st)
['(3', '+ ', '44', ')*', '5 ', '/ ', '7']
</code></pre>

<p>But I need to capture the parentheses in <code>'(3'</code> and <code>')*'</code> separately as well. </p></li>
<li><pre><code>&gt;&gt;&gt; re.findall("[()\d+\-*/]?", st)    
['(', '3', '', '+', '', '4', '4', ')', '*', '', '5', '', '/', '', '7', '']
</code></pre>

<p>This gives tons of blank tokens.</p></li>
</ol>

<p>You can't use multi-character constructs like <code>\d+</code> in a character class.</p>

<p>So you can do it by brute force like this:</p>

<pre><code>re.findall(r"\(|\)|\d+|-|\*|/", st)
</code></pre>

<p>Or you can use a character class for single-character tokens, alternated with other things:</p>

<pre><code>re.findall(r"[()\-*/]|\d+", st)
</code></pre>

python regex tokenize