Split a string by spaces — preserving quoted substrings — in Python

后端 未结 16 873
心在旅途
心在旅途 2020-11-22 15:05

I have a string which is like this:

this is \"a test\"

I\'m trying to write something in Python to split it up by space while ignoring spac

16条回答
  •  天涯浪人
    2020-11-22 15:32

    I see regex approaches here that look complex and/or wrong. This surprises me, because regex syntax can easily describe "whitespace or thing-surrounded-by-quotes", and most regex engines (including Python's) can split on a regex. So if you're going to use regexes, why not just say exactly what you mean?:

    test = 'this is "a test"'  # or "this is 'a test'"
    # pieces = [p for p in re.split("( |[\\\"'].*[\\\"'])", test) if p.strip()]
    # From comments, use this:
    pieces = [p for p in re.split("( |\\\".*?\\\"|'.*?')", test) if p.strip()]
    

    Explanation:

    [\\\"'] = double-quote or single-quote
    .* = anything
    ( |X) = space or X
    .strip() = remove space and empty-string separators
    

    shlex probably provides more features, though.

提交回复
热议问题