I have a string line like this:
valueA,{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3",.....,"propertyn":"valueN"},valueB,{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3",.....,"propertyn":"valueN"},valueC,{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3",.....,"propertyn":"valueN"}
That I want to split by commas
the above string should split into:
valueA
{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3",.....,"propertyn":"valueN"}
valueB
{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3",.....,"propertyn":"valueN"}
valueC
{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3",.....,"propertyn":"valueN"}
Try it but It doesn't split well.
String[] tokens = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
How can I do this?
You could use a simpler approach to use split with ",{" as the separator.
String[] tokens = line.split(",{");
The result would be as follows:
tokens[0] = "valueA"
tokens[1] = "property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3",.....,"propertyn":"valueN"}
So the only work left would be to add the starting bracket {
Instead of splitting try to extract the data with a pattern matcher.
String line = "valueA,{\"property1\":\"value1\",\"property2\":\"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]\",\"property3\":\"value3\"},valueB,{\"property1\":\"value1\",\"property2\":\"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]\",\"property3\":\"value3\"},valueC,{\"property1\":\"value1\",\"property2\":\"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]\",\"property3\":\"value3\"}";
Pattern pattern = Pattern.compile("([,]*(value[A-Z])),(\\{\"[\\w\":,\\[\\].]+\"\\})");
Matcher matcher = pattern.matcher(line);
List<String> data = new ArrayList<>();
while (matcher.find()) {
String key = matcher.group(2);
String value = matcher.group(3);
data.add(key);
data.add(value);
System.out.println(key);
System.out.println(value);
}
String[] array = data.toArray(new String[0]);
Resulting output:
valueA
{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3"}
valueB
{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3"}
valueC
{"property1":"value1","property2":"[[[1.5,1.7],[1.9,0.7],.....,[0.5,0.9]]]","property3":"value3"}
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.