Java regex: split comma-separated values but ignor

2020-03-31 05:22发布

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?

标签: java regex
6条回答
虎瘦雄心在
2楼-- · 2020-03-31 05:39

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.

查看更多
狗以群分
3楼-- · 2020-03-31 05:42

You could get every part that start with an " and finish with another " and then substring the first and last character of each part.

查看更多
够拽才男人
4楼-- · 2020-03-31 05:50

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));
    }
查看更多
Juvenile、少年°
5楼-- · 2020-03-31 05:50

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);
查看更多
神经病院院长
6楼-- · 2020-03-31 05:51
"[^"]*"

seems it is! in java string you can use

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

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

查看更多
放我归山
7楼-- · 2020-03-31 05:54

you could try this [^\"]+(?<!\",?)

查看更多
登录 后发表回答