简体   繁体   English

如何在Java中使用多个定界符分隔字符串并且没有空白?

[英]How to separate a string in java using multiple delimiters and with no white space available?

how can i separate a string like this in java? 我如何在Java中分离这样的字符串? Quotes are not seperate. 行情不是分开的。 The following code is entirely a string along with println. 以下代码与println一起完全是一个字符串。 And i need to separate it into tokens as in the output. 我需要将其分成令牌,如输出所示。

println "How are you",Sir, "Hope you are doing good","?"

Note: There are no whitespaces between you" and , and so on.. output: 注意:“和之间没有空格,依此类推..输出:

println
"how are you"
,
Sir
,
"Hope you are doing good"
,
"?"

Dont know if it is optimised but it will give your desired results. 不知道它是否经过优化,但是会得到您想要的结果。

Pattern pattern = Pattern.compile("(\"[^\"]*\")|(\\w+)|(,)");
        Matcher matcher = pattern.matcher(string);
        while(matcher.find()){
            if(matcher.group(2)!=null)
            System.out.println(matcher.group(2));
            if(matcher.group(1)!=null)
            System.out.println(matcher.group(1));
            if(matcher.group(3)!=null)
            System.out.println(matcher.group(3));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM