By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,769 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,769 IT Pros & Developers. It's quick & easy.

String split with " and/or ' and/or \<sep>

P: n/a
How to (super)split a string (literal) containing " and/or ' and/or \<sep>.

example:

' a " b b " c\ c '.supersplit(' ')
->
['a', ' b b ', 'c c']
Thanks and Grüessli
--
Kurt Müller:
ku**********@aerodynamics.ch

Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Jun 24, 3:56*am, Kurt Mueller <kurt.muel...@aerodynamics.chwrote:
How to (super)split a string (literal) containing " and/or ' and/or \<sep>.

example:

' a *" *b b * " *c\ c '.supersplit(' ')
->
['a', ' *b b * ', 'c c']

Thanks and Grüessli
--
Kurt Müller:
kurt.muel...@aerodynamics.ch
>>re.split(r'''['"\\]''',' a " b b " c\ c ')
[' a ', ' b b ', ' c', ' c ']
Jun 27 '08 #2

P: n/a
On Jun 24, 3:56*am, Kurt Mueller <kurt.muel...@aerodynamics.chwrote:
How to (super)split a string (literal) containing " and/or ' and/or \<sep>.

example:

' a *" *b b * " *c\ c '.supersplit(' ')
->
['a', ' *b b * ', 'c c']

Thanks and Grüessli
--
Kurt Müller:
kurt.muel...@aerodynamics.ch
Or did you mean this?
>>re.split(r'''['"]|\\ ''',' a " b b " c\ c ')
[' a ', ' b b ', ' c', 'c ']

(In your example, you should prefix you quoted string literal with an
r, as in:

r' a " b b " c\ c '.supersplit(' ')

That way, the '\' character will be treated as just any other
character.

-- Paul

Jun 27 '08 #3

P: n/a
Kurt Mueller wrote:
How to (super)split a string (literal) containing " and/or ' and/or
\<sep>.

example:

' a " b b " c\ c '.supersplit(' ')
->
['a', ' b b ', 'c c']
Thanks and Grüessli
>>import shlex
shlex.split(' a " b b " c\ c ')
['a', ' b b ', 'c c']

Peter
Jun 27 '08 #4

P: n/a
Peter Otten schrieb:
Kurt Mueller wrote:
>How to (super)split a string (literal) containing " and/or ' and/or
\<sep>.
example:

' a " b b " c\ c '.supersplit(' ')
->
['a', ' b b ', 'c c']
>>>import shlex
shlex.split(' a " b b " c\ c ')
['a', ' b b ', 'c c']
Thanks Peter
Thanks Paul

shlex is what I was looking for.
Grüessli
--
Kurt Müller, mu@problemlos.ch

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.