Skip to content

Instantly share code, notes, and snippets.

@ljmccarthy
Created August 6, 2014 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmccarthy/94acbdc29b5cf0f96f97 to your computer and use it in GitHub Desktop.
Save ljmccarthy/94acbdc29b5cf0f96f97 to your computer and use it in GitHub Desktop.
Split string without splitting embedded quoted strings and maintaining quotes.
import re
re_quoted_string = re.compile(r"(\"(?:\\\"|[^\"])*\"|'(?:\\'|[^'])*')")
def split_with_quoted_strings(s):
"""Split string without splitting embedded quoted strings."""
return [x.strip() for x in re_quoted_string.split(s) if x.strip()]
for x in split_with_quoted_strings('hello "world \\\"blah" \'x y z\' blah'):
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment