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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kurt Mueller

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

    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.mueller@ae rodynamics.ch

  • Paul McGuire

    #2
    Re: String split with &quot; and/or ' and/or \&lt;sep&gt;

    On Jun 24, 3:56 am, Kurt Mueller <kurt.muel...@a erodynamics.chw rote:
    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...@ae rodynamics.ch
    >>re.split(r' ''['"\\]''',' a " b b " c\ c ')
    [' a ', ' b b ', ' c', ' c ']

    Comment

    • Paul McGuire

      #3
      Re: String split with &quot; and/or ' and/or \&lt;sep&gt;

      On Jun 24, 3:56 am, Kurt Mueller <kurt.muel...@a erodynamics.chw rote:
      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...@ae rodynamics.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

      Comment

      • Peter Otten

        #4
        Re: String split with &quot; and/or ' and/or \&lt;sep&gt;

        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

        Comment

        • Kurt Mueller

          #5
          Re: String split with &quot; and/or ' and/or \&lt;sep&gt;

          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.c h

          Comment

          Working...