Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > is there any lib can split string in this way?

Reply
Thread Tools

is there any lib can split string in this way?

 
 
Leo Jay
Guest
Posts: n/a
 
      01-09-2006
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

--
Best Regards,
Leo Jay
 
Reply With Quote
 
 
 
 
bonono@gmail.com
Guest
Posts: n/a
 
      01-09-2006
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.

Leo Jay 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?
>
> thanks
>
> --
> Best Regards,
> Leo Jay


 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      01-09-2006
On Mon, 9 Jan 2006 13:59:44 +0800, Leo Jay <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
any python wrapper to call .lib static library(ufmod.lib)? est Python 1 02-16-2008 10:56 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
install_driver(Oracle) failed: Can't load 'C:/Perl/site/lib/auto/DBD/Oracle/Oracle.dll' for module DBD::Oracle: load_file:The specified procedure could not be found at C:/Perl/lib/DynaLoader.pm line 230. Feyruz Perl Misc 4 10-14-2005 06:47 PM



Advertisments