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

Paul McGuire ptmcg at austin.rr.com
Tue Jun 24 11:34:34 CEST 2008


On Jun 24, 3:56 am, Kurt Mueller <kurt.muel... at aerodynamics.ch> 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
> --
> Kurt Müller:
> kurt.muel... at 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




More information about the Python-list mailing list