0

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!

Kevin Holtkamp
  • 479
  • 4
  • 17
  • 1
    Please give a more thorough example and expected output. For example, your title talks about spaces within quotes and outside quotes, but your current sample does not have both of these cases. – Bohemian Jun 02 '20 at 19:50
  • You might try it with lookarounds, but this behaviour can be error prone with unbalanced double quotes `(?:\h|(?=")|(?<="))(?=(?:[^"]*"[^"]*")*[^"]*$)` https://regex101.com/r/otbMEQ/1 – The fourth bird Jun 02 '20 at 20:00
  • Does this answer your question? [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – fjsv Jun 05 '20 at 12:40

1 Answers1

0

I found a solution that is simple and works for my case:

    /[^"\s]+|"[^"]*"/
Kevin Holtkamp
  • 479
  • 4
  • 17