is there any lib can split string in this way?

Bengt Richter bokr at oz.net
Mon Jan 9 02:26:15 EST 2006


On Mon, 9 Jan 2006 13:59:44 +0800, Leo Jay <python.leojay at gmail.com> wrote:

>I want to split a string like this:
>'abc  def  "this is a test"  ok'
>into:
>['abc', 'def', 'this is a test', 'ok']
>
>is there any lib meet my need?
>
Narrowly interpreting your requirements (only quotes are with
double quotes (never containing escaped same) and strip quotes off)
and tested only as you see ;-)

 >>> import re
 >>> rx = re.compile(r'"([^"]*)"|(\w+)')
 >>> s = 'abc  def  "this is a test"  ok'
 >>> [a or b for a,b in rx.findall(s)]
 ['abc', 'def', 'this is a test', 'ok']

Regards,
Bengt Richter



More information about the Python-list mailing list