Try the following:
[ ]+(?=([^"]*"[^"]*")*[^"]*$)
which will be split into one or more spaces only if these spaces are followed by zero or an even number of quotation marks (up to the end of the line!).
The following demo:
public class Main { public static void main(String[] args) { String text = "a \"bcd\" e \"fg\" h"; System.out.println("text = " + text + "\n"); for(String t : text.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)")) { System.out.println(t); } } }
outputs the following result:
text = a "bcd" e "fg" h
a
"bcd"
e
"fg"
h
source share