i would like to split a String at every space character, but split the quoted parts out seperately. I tried using the regex pattern
",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"
and replaced the commas ","
with space " " or "\\s"
"\\s(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"
and it works fine in the following example:
Input: "foo \"bar foo\" bar"
Output:[foo, "bar foo", bar]
but if you remove the space before and after the inner quotes, you get the following:
Input: "bar foo\"bar foo\"bar foo"
Output: ["bar", "foo\"bar foo\"bar", "foo"]
Desired Output: ["bar", "foo", "\"bar foo\"", "bar", "foo"]
@the-fourth-bird kindly created the following regex
"(?:\\h|(?=\")|(?<=\"))(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"
which works in the above situation, but fails in the case of spaces:
Input: "foo \"bar foo\" bar"
Output: ["foo", "", "\"bar foo\"", "bar"]
Desired Output: ["food", "\"bar foo\"", "bar"]
This problem can be solved by just removing empty strings from the array, but is there also a way to solve it in the regex?
Thanks a lot for the help!