Tokenise line containing string literals
<p>Using <code>str.split</code> on <code>"print 'Hello, world!' times 3"</code> returns the list <code>["print", "'Hello,", "world!'", "times", "3"]</code>. However, I want the result <code>["print", "'Hello, world!'", "times", "3"]</code>. How can I do that?</p>

<p>This regex will capture the quotes, if you want them.</p>

<pre><code>import re

s = "print 'hello, world!' 3 times"
re.findall(r'(\w+|\'.+\')',s)
</code></pre>

python string parsing