简体   繁体   English

正则表达式Java拆分逗号分隔的字符串但忽略引号+大括号+递归括号内的逗号

[英]regex Java splitting a comma-separated String but ignoring commas within quotes+braces+recursive brackets

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.您可以使用更简单的方法将 split 与 ",{" 作为分隔符。

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"}

暂无
暂无

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

相关问题 Java:拆分逗号分隔的字符串但忽略引号中的逗号 - Java: splitting a comma-separated string but ignoring commas in quotes Java:拆分逗号分隔的字符串但忽略括号中的逗号 - Java: splitting a comma-separated string but ignoring commas in parentheses Java正则表达式:拆分逗号分隔的值,但忽略引号中的逗号 - Java regex: split comma-separated values but ignore commas in quotes 正则表达式匹配以逗号分隔的字符串中的元素,忽略双引号、大括号和方括号内的逗号 - RegEx to match elements in a string separated by comma ignoring comma inside double quotes, curly brackets and square brackets Java ArrayList<string> 在方括号中形成逗号分隔的字符串</string> - Java ArrayList<String> form comma-separated strings in square brackets Java Regex - 拆分逗号分隔列表但排除方括号内的逗号 - Java Regex - Split comma separated list but exclude commas within square brackets 用引号和引号内的逗号分隔逗号分隔的字符串,并在引号内使用转义引号 - Split comma separated string with quotes and commas within quotes and escaped quotes within quotes 拆分空格分隔的字符串,忽略单引号内的空格 - splitting a whitespace separated string, ignoring whitespace within single quotes Java:拆分分号分隔的字符串但忽略引号中的转义+分号 - Java: splitting a semicolon separated string but ignoring escape+semicolon in quotes Java Regex - 拆分逗号分隔列表,但在括号内排除逗号 - Java Regex - split comma separated list, but exclude commas within parentheses
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM