Splitting with quoted strings?

Jonathan Feinberg jdf at pobox.com
Tue May 8 20:18:27 CEST 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.


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\""',

for t in tests:
	print qsplit(t)

Jonathan Feinberg   jdf at pobox.com   Sunny Brooklyn, NY

More information about the Python-list mailing list