Splitting with quoted strings?
Jonathan Feinberg
jdf at pobox.com
Tue May 8 14:18:27 EDT 2001
Chris Barker <chrishbarker at home.net> writes:
> what I want is (for example):
>
> 'this that "the other" some more'.split()
>
> to give me ['this', 'that', 'the other', 'some', 'more']
I couldn't resist a regex solution to your problem. In coming up with
it, though, I discovered that the sre version that comes with the
ActiveState 2.0 distro has a bug. I downloaded the latest sre from
PythonWare, and it works as it should.
http://www.pythonware.com/products/sre/index.htm
Here is my code:
#------------------------------------------------------------
import re
qsplitter = re.compile(r"""
(?:
(?: " ((?: \\.|[^\"])*) " ) # a quoted string
| # or
(\S+) # some non-whitespace
) \s* # followed by some whitespace
""", re.X)
def qsplit(text):
return [ x[0] or x[1] for x in qsplitter.findall(text) ]
tests = [ 'sample line "with quoted" text',
'line without quoted test',
'"line with" "quoted text" again',
r'"line with" "escaped \"quotes\""',
'',
'line'
]
for t in tests:
print qsplit(t)
#------------------------------------------------------------
--
Jonathan Feinberg jdf at pobox.com Sunny Brooklyn, NY
http://pobox.com/~jdf
More information about the Python-list
mailing list