Created
August 6, 2014 15:18
-
-
Save ljmccarthy/94acbdc29b5cf0f96f97 to your computer and use it in GitHub Desktop.
Split string without splitting embedded quoted strings and maintaining quotes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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