title: python divide string by space except when in quote, also allow escaped quotes single and double
body: <p>First: I know there are much examples on stackoverflow for these kind of problems but i have not found the exact result I need.</p>
  
  <p>I have the following program</p>
  
  <pre><code>import re
  
  def magic(string):     
      return [i.strip('"').strip("'") for i in re.split(r'(\s+|(?&lt;!\\)".*?(?&lt;!\\)"|(?&lt;!\\)\'.*?(?&lt;!\\)\')', string) if i.strip()] 
  
  myString = """ function_name param1 param 2 "param 3" "param \"4 lorem ipsum\""  'param 5' 'param \'6 lorem ipsum\'' """
  print magic(myString)
  </code></pre>
  
  <p>I expected this as output:</p>
  
  <pre><code>['function_name', 'param1', 'param', '2', 'param 3', 'param "4 lorem ipsum"', 'param 5', "param '6 lorem ipsum'"]
  </code></pre>
  
  <p>But I get this:</p>
  
  <pre><code>['function_name', 'param1', 'param', '2', 'param 3', 'param ', '4', 'lorem', 'ipsum', '', 'param 5', 'param ', '6', 'lorem', 'ipsum', '']
  </code></pre>
  
  <p>So Python has problems with the escaping. 
  When I trible escape my strings..</p>
  
  <pre><code>myString = """ function_name param1 param 2 "param 3" "param \\\"4 lorem ipsum\\\""  'param 5' 'param \\\'6 lorem ipsum\\\'' """
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>['function_name', 'param1', 'param', '2', 'param 3', 'param \\"4 lorem ipsum\\', 'param 5', "param \\'6 lorem ipsum\\"]
  </code></pre>
  
  <p>=> Some quotes disappear and anyway I dont want to write three \ to escape a quote.</p>
  
  <p>I hope someone can help me to improve this function </p>