Regex for splitting a string using space when not surrounded by single or double quotes

后端 未结 15 2136
梦毁少年i
梦毁少年i 2020-11-22 03:15

I\'m new to regular expressions and would appreciate your help. I\'m trying to put together an expression that will split the example string using all spaces that are not s

15条回答
  •  礼貌的吻别
    2020-11-22 03:38

    You can also try this:

        String str = "This is a string that \"will be\" highlighted when your 'regular expression' matches something";
        String ss[] = str.split("\"|\'");
        for (int i = 0; i < ss.length; i++) {
            if ((i % 2) == 0) {//even
                String[] part1 = ss[i].split(" ");
                for (String pp1 : part1) {
                    System.out.println("" + pp1);
                }
            } else {//odd
                System.out.println("" + ss[i]);
            }
        }
    

提交回复
热议问题