is there any lib can split string in this way?

L

Leo Jay

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?

thanks
 
B

bonono

This has been asked not long ago. the shlex module as well as csv
module both should be able to handle it. for this simple case
shlex.split() seems to be the easiest.
 
B

Bengt Richter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top