Separation in space, if not between quotation marks

I tried this:

+|(?!(\"[^"]*\")) 

but it didn’t work. what can i do to make it work? btw im using java string.split ().

+4
source share
3 answers

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
+17
source

It works?

 var str="Hi there" var splitOutput=str.split(" "); //splitOutput[0]=Hi //splitOutput[1]=there 

Sorry, I misunderstood your question. Add this from Bart’s explanation \s(?=([^"]*"[^"]*")*[^"]*$) Or [ ]+(?=([^"]*"[^"]*")*[^"]*$)

0
source

Is this what you are looking for?

 input.split("(?<!\") (?!\")") 
0
source

All Articles