Yet another "split string by spaces preserving single quotes" problem

Massi massi_srb at msn.com
Sun May 13 17:14:58 EDT 2012


Hi everyone,
I know this question has been asked thousands of times, but in my case
I have an additional requirement to be satisfied. I need to handle
substrings in the form 'string with spaces':'another string with
spaces' as a single token; I mean, if I have this string:

s ="This is a 'simple test':'string which' shows 'exactly my'
problem"

I need to split it as follow (the single quotes must be mantained in
the splitted list):

["This", "is",  "a",  "'simple test':'string which'", "shows",
"'exactly my'", "problem"]

Up to know I have written some ugly code which uses regular
expression:

splitter = re.compile("(?=\s|^)('[^']+') | ('[^']+')(?=\s|$)")

temp = [t for t in splitter.split(s) if t not in [None, '']]
print temp
t = []
for i, p in enumerate(temp) :
    for x in ([p] if (p[0] == "'" and p[1] == "'") else p.split('
')) :
        t.append(x)

But it does not handle "colon" case.
Any hints? Thanks in advance!



More information about the Python-list mailing list