英文:
RegEx to match elements in a string separated by comma ignoring comma inside double quotes, curly brackets and square brackets
问题
我有一个以下的string
值:
String str = {"A":"不要分隔,值","B":"OPM","C":[1,2,"AB",{"1":{"1":2,"2":[1,2]},"2":2}],"D":{"1":1,"2":[{"1":1,"2":2,"3":3},9,10]}};
我该如何编写一个正则表达式来捕获其中由不在双引号、方括号或花括号内的逗号分隔的元素?我想通过类似以下的方式匹配并获取这些元素,使用模式匹配。
List<String> list = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
list.add(matcher.group());
}
元素应为:
"A":"不要分隔,值"
"B":"OPM"
"C":[1,2,"AB",{"1":{"1":2,"2":[1,2]},"2":2}]
"D":{"1":1,"2":[{"1":1,"2":2,"3":3},9,10]}
如果字符串如下所示:
String str = [1,2,{"A":1,"B":2},[19,10,11,{"A":1,"B":2}],100];
那么元素应为:
1
2
{"A":1,"B":2}
[19,10,11,{"A":1,"B":2}]
100
英文:
I have a string
value as below
String str = {"A":"do not seperate,value","B":"OPM","C":[1,2,"AB",{"1":{"1":2,"2":[1,2]},"2":2}],"D":{"1":1,"2":[{"1":1,"2":2,"3":3},9,10]}};
How can I write a regular expression to capture its elements separated by a comma which is not inside double quotes, square brackets or curly brackets? I want to match and get the elements by doing something like below; using pattern matching.
List<String> list = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
list.add(matcher.group());
}
The elements should be,
"A":"do not seperate,value"
"B":"OPM"
"C":[1,2,"AB",{"1":{"1":2,"2":[1,2]},"2":2}]
"D":{"1":1,"2":[{"1":1,"2":2,"3":3},9,10]}
If the string is something like below
String str = [1,2,{"A":1,"B":2},[19,10,11,{"A":1,"B":2}],100]
Then the elements should be
1
2
{"A":1,"B":2}
[19,10,11,{"A":1,"B":2}]
100
答案1
得分: 1
由于这是一个 JSON 字符串,您可以使用对象映射器解析它,并将其作为键值对存储在哈希映射中。
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.*;
import org.json.JSONObject;
public class Test {
public static void main(String args[]) throws IOException {
String str = "{\"A\":\"不要分开,数值\",\"B\":\"OPM\",\"C\":[1,2,\"AB\",{\"1\":{\"1\":2,\"2\":[1,2]},\"2\":2}],\"D\":{\"1\":1,\"2\":[{\"1\":1,\"2\":2,\"3\":3},9,10]}}";
JSONObject obj = new JSONObject(str);
HashMap<String, Object> result = new ObjectMapper().readValue(str, new TypeReference<Map<String, Object>>() {
});
result.entrySet().stream().forEach(System.out::println);
}
}
**输出**
A=不要分开,数值
B=OPM
C=[1, 2, AB, {1={1=2, 2=[1, 2]}, 2=2}]
D={1=1, 2=[{1=1, 2=2, 3=3}, 9, 10]}
英文:
Since its a json string you can parse it with object mapper and get them as key value pair in a hashmap.
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.*;
import org.json.JSONObject;
public class Test {
public static void main(String args[]) throws IOException {
String str = "{\"A\":\"do not seperate,value\",\"B\":\"OPM\",\"C\":[1,2,\"AB\",{\"1\":{\"1\":2,\"2\":[1,2]},\"2\":2}],\"D\":{\"1\":1,\"2\":[{\"1\":1,\"2\":2,\"3\":3},9,10]}}";
JSONObject obj = new JSONObject(str);
HashMap<String, Object> result = new ObjectMapper().readValue(str, new TypeReference<Map<String, Object>>() {
});
result.entrySet().stream().forEach(System.out::println);
}
}
output
A=do not seperate,value
B=OPM
C=[1, 2, AB, {1={1=2, 2=[1, 2]}, 2=2}]
D={1=1, 2=[{1=1, 2=2, 3=3}, 9, 10]}
答案2
得分: 1
public static List<String> getStringElements(String str) {
List<String> elementsList = new ArrayList<>();
StringBuilder element = new StringBuilder();
int bracketsCount = 0;
int quotesCount = 0;
char[] strChars = str.substring(1, str.length() - 1).toCharArray();
for (char strChar : strChars) {
element.append(strChar);
if (strChar == '\"') {
quotesCount++;
} else if (strChar == '[' && quotesCount % 2 == 0) {
bracketsCount++;
} else if (strChar == '{' && quotesCount % 2 == 0) {
bracketsCount++;
} else if (strChar == '(' && quotesCount % 2 == 0) {
bracketsCount++;
} else if (strChar == ']' && quotesCount % 2 == 0) {
bracketsCount--;
} else if (strChar == '}' && quotesCount % 2 == 0) {
bracketsCount--;
} else if (strChar == ')' && quotesCount % 2 == 0) {
bracketsCount--;
} else if (strChar == ',' && bracketsCount == 0 && quotesCount % 2 == 0) {
elementsList.add(element.substring(0, element.length() - 1));
element = new StringBuilder();
}
}
if (element.length() > 0) {
elementsList.add(element.toString());
}
return elementsList;
}
英文:
You probably can do something like this
public static List<String> getStringElements(String str) {
List<String> elementsList = new ArrayList<>();
StringBuilder element = new StringBuilder();
int bracketsCount = 0;
int quotesCount = 0;
char[] strChars = str.substring(1, str.length() - 1).toCharArray();
for (char strChar : strChars) {
element.append(strChar);
if (strChar == '\"') {
quotesCount++;
} else if (strChar == '[' && quotesCount % 2 == 0) {
bracketsCount++;
} else if (strChar == '{' && quotesCount % 2 == 0) {
bracketsCount++;
} else if (strChar == '(' && quotesCount % 2 == 0) {
bracketsCount++;
} else if (strChar == ']' && quotesCount % 2 == 0) {
bracketsCount--;
} else if (strChar == '}' && quotesCount % 2 == 0) {
bracketsCount--;
} else if (strChar == ')' && quotesCount % 2 == 0) {
bracketsCount--;
} else if (strChar == ',' && bracketsCount == 0 && quotesCount % 2 == 0) {
elementsList.add(element.substring(0, element.length() - 1));
element = new StringBuilder();
}
}
if (element.length() > 0) {
elementsList.add(element.toString());
}
return elementsList;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论