简体   繁体   中英

Java regex: split comma-separated values but ignore commas in quotes

I have text as follows:

"text","1","more, more text","3"

Could anyone kindly show me what regex delimeters I have to use to get the following:

text
1
more, more text
3

I was reading the Sun tutorial here , up until "Methods of the matcher class" but I am still at a loss. Thanks!

If it were something like text,1,more it would be easy enough, but unfortunately it's not like that. Any ideas?

Try this pattern: "(.+?)"

It will match 1 or more characters between double quotes. The part of the text between the quotes is available as matcher.group(1) .

Look at the javadoc for Pattern class to learn more. Also, look at matcher.find() method.

您可以尝试此[^\\"]+(?<!\\",?)

You can either go straight for the split() method like this:

    String text = "\"text\",\"1\",\"more, more text\",\"3\"";

    String[] split = text.split("\"(,\")?");
    for (String string : split) {
        System.out.println(string);
    }

(beware that this returns a length 5 array, with the first position being an empty string)

Or, if you want to use a Pattern/Matcher, you can do it like this:

    Pattern pattern = Pattern.compile("\"([^\"]+)\"");
    Matcher matcher = pattern.matcher(text);
    while(matcher.find()){
        System.out.println(matcher.group(1));
    }

你可以得到所有以一个开始部分"并与另一完成" ,然后串各部分的第一个和最后一个字符。

You can try something like that. I know, it is not good solution, but it can help you :)

string[] s=text.split("\",\""); 
s[0]=s[0].substring(1);
s[s.length-1]=s[s.length-1].substring(0,s[s.length-1].length);
"[^"]*"

seems it is! in java string you can use

"\"[^\"]*\""

You can test it online here: http://www.regexplanet.com/simple/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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