开发者

Splitting at space if not between quotes

开发者 https://www.devze.com 2023-03-04 15:23 出处:网络
i tried this: +|(?!(\\\"[^\"]*\\\")) but it didn\'t work. what can i else do 开发者_JAVA技巧to make it work?

i tried this:

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

but it didn't work. what can i else do 开发者_JAVA技巧to make it work? btw im using java's string.split().


Try this:

[ ]+(?=([^"]*"[^"]*")*[^"]*$)

which will split on one or more spaces only if those spaces are followed by zero, or an even number of quotes (all the way to the end of the string!).

The following demo:

public class Main {
    public static void main(String[] args) {
        String text = "a \"b c d\" e \"f g\" h";
        System.out.println("text = " + text + "\n");
        for(String t : text.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)")) {
            System.out.println(t);
        }
    }
}

produces the following output:

text = a "b c d" e "f g" h

a
"b c d"
e
"f g"
h


Is this what you are looking for?

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


Does this work?

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号