Splitting a string by spaces without space-splitting elements in double-quotes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
9
down vote

favorite












I need to split a command like:



r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'`


into:



['"C:\Program Files (x86)\myeditor"',
'"$FILEPATH"',
'-n$LINENO',
'"c:\Program Files"',
'-f$FILENAME',
'-aArg2']


That is, I want to split by spaces, but avoid splitting the elements in double-quotes.



I have this code:



import re

s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

start = end = 0
split =

for elem in re.findall('".*?"', s):
end = s.find(elem)
split.append(s[start:end])
split.append(elem)
start = end + len(elem)

split.extend(s[start:].split())
split = [elem.strip() for elem in split]
split = list(filter(None, split))


It works, but I'm wondering if there's some more elegant/shorter/more readable way to do that in Python(3) ?







share|improve this question















  • 1




    Already answered: stackoverflow.com/q/366202/823470
    – tar
    Apr 6 at 14:58







  • 2




    @tar That answer is on Java and some complex regexes. I think that shlex + copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
    – LetMeSOThat4U
    Apr 7 at 11:56
















up vote
9
down vote

favorite












I need to split a command like:



r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'`


into:



['"C:\Program Files (x86)\myeditor"',
'"$FILEPATH"',
'-n$LINENO',
'"c:\Program Files"',
'-f$FILENAME',
'-aArg2']


That is, I want to split by spaces, but avoid splitting the elements in double-quotes.



I have this code:



import re

s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

start = end = 0
split =

for elem in re.findall('".*?"', s):
end = s.find(elem)
split.append(s[start:end])
split.append(elem)
start = end + len(elem)

split.extend(s[start:].split())
split = [elem.strip() for elem in split]
split = list(filter(None, split))


It works, but I'm wondering if there's some more elegant/shorter/more readable way to do that in Python(3) ?







share|improve this question















  • 1




    Already answered: stackoverflow.com/q/366202/823470
    – tar
    Apr 6 at 14:58







  • 2




    @tar That answer is on Java and some complex regexes. I think that shlex + copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
    – LetMeSOThat4U
    Apr 7 at 11:56












up vote
9
down vote

favorite









up vote
9
down vote

favorite











I need to split a command like:



r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'`


into:



['"C:\Program Files (x86)\myeditor"',
'"$FILEPATH"',
'-n$LINENO',
'"c:\Program Files"',
'-f$FILENAME',
'-aArg2']


That is, I want to split by spaces, but avoid splitting the elements in double-quotes.



I have this code:



import re

s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

start = end = 0
split =

for elem in re.findall('".*?"', s):
end = s.find(elem)
split.append(s[start:end])
split.append(elem)
start = end + len(elem)

split.extend(s[start:].split())
split = [elem.strip() for elem in split]
split = list(filter(None, split))


It works, but I'm wondering if there's some more elegant/shorter/more readable way to do that in Python(3) ?







share|improve this question











I need to split a command like:



r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'`


into:



['"C:\Program Files (x86)\myeditor"',
'"$FILEPATH"',
'-n$LINENO',
'"c:\Program Files"',
'-f$FILENAME',
'-aArg2']


That is, I want to split by spaces, but avoid splitting the elements in double-quotes.



I have this code:



import re

s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

start = end = 0
split =

for elem in re.findall('".*?"', s):
end = s.find(elem)
split.append(s[start:end])
split.append(elem)
start = end + len(elem)

split.extend(s[start:].split())
split = [elem.strip() for elem in split]
split = list(filter(None, split))


It works, but I'm wondering if there's some more elegant/shorter/more readable way to do that in Python(3) ?









share|improve this question










share|improve this question




share|improve this question









asked Apr 6 at 9:22









LetMeSOThat4U

370129




370129







  • 1




    Already answered: stackoverflow.com/q/366202/823470
    – tar
    Apr 6 at 14:58







  • 2




    @tar That answer is on Java and some complex regexes. I think that shlex + copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
    – LetMeSOThat4U
    Apr 7 at 11:56












  • 1




    Already answered: stackoverflow.com/q/366202/823470
    – tar
    Apr 6 at 14:58







  • 2




    @tar That answer is on Java and some complex regexes. I think that shlex + copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
    – LetMeSOThat4U
    Apr 7 at 11:56







1




1




Already answered: stackoverflow.com/q/366202/823470
– tar
Apr 6 at 14:58





Already answered: stackoverflow.com/q/366202/823470
– tar
Apr 6 at 14:58





2




2




@tar That answer is on Java and some complex regexes. I think that shlex + copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
– LetMeSOThat4U
Apr 7 at 11:56




@tar That answer is on Java and some complex regexes. I think that shlex + copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
– LetMeSOThat4U
Apr 7 at 11:56










2 Answers
2






active

oldest

votes

















up vote
11
down vote



accepted










The best way to do what you want with the standard library would be shlex.split():



>>> import shlex
>>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
>>> shlex.split(s)
['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']


Note that the quotes are not retained.






share|improve this answer




























    up vote
    4
    down vote













    You could use a different regex:



    import re

    s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

    pattern = re.compile(r"(("[^"]+")|(-[^s]+))")

    for m in re.finditer(pattern, s):
    print(m.group(0))


    This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).



    However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.






    share|improve this answer























      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "196"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191391%2fsplitting-a-string-by-spaces-without-space-splitting-elements-in-double-quotes%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      11
      down vote



      accepted










      The best way to do what you want with the standard library would be shlex.split():



      >>> import shlex
      >>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
      >>> shlex.split(s)
      ['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']


      Note that the quotes are not retained.






      share|improve this answer

























        up vote
        11
        down vote



        accepted










        The best way to do what you want with the standard library would be shlex.split():



        >>> import shlex
        >>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
        >>> shlex.split(s)
        ['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']


        Note that the quotes are not retained.






        share|improve this answer























          up vote
          11
          down vote



          accepted







          up vote
          11
          down vote



          accepted






          The best way to do what you want with the standard library would be shlex.split():



          >>> import shlex
          >>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
          >>> shlex.split(s)
          ['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']


          Note that the quotes are not retained.






          share|improve this answer













          The best way to do what you want with the standard library would be shlex.split():



          >>> import shlex
          >>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
          >>> shlex.split(s)
          ['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']


          Note that the quotes are not retained.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Apr 6 at 11:28









          etene

          2916




          2916






















              up vote
              4
              down vote













              You could use a different regex:



              import re

              s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

              pattern = re.compile(r"(("[^"]+")|(-[^s]+))")

              for m in re.finditer(pattern, s):
              print(m.group(0))


              This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).



              However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.






              share|improve this answer



























                up vote
                4
                down vote













                You could use a different regex:



                import re

                s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

                pattern = re.compile(r"(("[^"]+")|(-[^s]+))")

                for m in re.finditer(pattern, s):
                print(m.group(0))


                This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).



                However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.






                share|improve this answer

























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  You could use a different regex:



                  import re

                  s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

                  pattern = re.compile(r"(("[^"]+")|(-[^s]+))")

                  for m in re.finditer(pattern, s):
                  print(m.group(0))


                  This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).



                  However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.






                  share|improve this answer















                  You could use a different regex:



                  import re

                  s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'

                  pattern = re.compile(r"(("[^"]+")|(-[^s]+))")

                  for m in re.finditer(pattern, s):
                  print(m.group(0))


                  This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).



                  However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Apr 6 at 19:11


























                  answered Apr 6 at 10:41









                  yuri

                  3,3862832




                  3,3862832






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191391%2fsplitting-a-string-by-spaces-without-space-splitting-elements-in-double-quotes%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Chat program with C++ and SFML

                      Function to Return a JSON Like Objects Using VBA Collections and Arrays

                      Will my employers contract hold up in court?