Joseph Caruso Joseph Caruso - 2 months ago 17
Python Question

Tokenise line containing string literals

Using

str.split
on
"print 'Hello, world!' times 3"
returns the list
["print", "'Hello,", "world!'", "times", "3"]
. However, I want the result
["print", "'Hello, world!'", "times", "3"]
. How can I do that?

Answer

This regex will capture the quotes, if you want them.

import re

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