gpt4 book ai didi

java - ["abc"、["123"、 "cba"的正则表达式]]

转载 作者:行者123 更新时间:2023-11-29 06:41:46 30 4
gpt4 key购买 nike

我不擅长正则表达式,所以任何帮助将不胜感激。

我需要解析这样的字符串:

["text", "text", ["text",["text"]],"text"]

输出应该是(4 个字符串):

text, text, ["text",["text"]], text

我试过这个模式 (\\[[^\\[,^\\]]*\\])|(\"([^\"]*)\"):

String data="\"aa\", \"aaa\", [\"bb\", [\"1\",\"2\"]], [cc]";
Pattern p=Pattern.compile("(\\[[^\\[,^\\]]*\\])|(\"([^\"]*)\")");

但是输出是(输出中的引号本身并不是那么重要):

"aa", "aaa", "bb", "1", "2", [cc]

如何改进我的正则表达式?

最佳答案

我不确定正则表达式是否能够自行完成此类工作。不过,这是一种方法:

// data string
String input = "\"aa\", \"a, aa\", [\"bb\", [\"1\", \"2\"]], [cc], [\"dd\", [\"5\"]]";
System.out.println(input);

// char that can't ever be within the data string
char tempReplacement = '#';
// escape strings containing commas, e.g "hello, world", ["x, y", 42]
while(input.matches(".*\"[^\"\\[\\]]+,[^\"\\[\\]]+\".*")) {
input = input.replaceAll("(\"[^\"\\[\\]]+),([^\"\\[\\]]+\")", "$1" + tempReplacement + "$2");
}
// while there are "[*,*]" substrings
while(input.matches(".*\\[[^\\]]+,[^\\]]+\\].*")) {
// replace the nested "," chars by the replacement char
input = input.replaceAll("(\\[[^\\]]+),([^\\]]+\\])", "$1" + tempReplacement + "$2");
}

// split the string by the remaining "," (i.e. those non nested)
String[] split = input.split(",");

List<String> output = new LinkedList<String>();
for(String s : split) {
// replace all the replacement chars by a ","
s = s.replaceAll(tempReplacement + "", ",");
s = s.trim();
output.add(s);
}

// syso
System.out.println("SPLIT:");
for(String s : output) {
System.out.println("\t" + s);
}

输出:

"aa", "a, aa", ["bb", ["1", "2"]], [cc], ["dd", ["5"]]
SPLIT:
"aa"
"a, aa"
["bb", ["1","2"]]
[cc]
["dd", ["5"]]

PS:代码看起来很复杂,因为有注释。这是一个更简洁的版本:

public static List<String> split(String input, char tempReplacement) {
while(input.matches(".*\"[^\"\\[\\]]+,[^\"\\[\\]]+\".*")) {
input = input.replaceAll("(\"[^\"\\[\\]]+),([^\"\\[\\]]+\")", "$1" + tempReplacement + "$2");
}
while(input.matches(".*\\[[^\\]]+,[^\\]]+\\].*")) {
input = input.replaceAll("(\\[[^\\]]+),([^\\]]+\\])", "$1" + tempReplacement + "$2");
}
String[] split = input.split(",");
List<String> output = new LinkedList<String>();
for(String s : split) {
output.add(s.replaceAll(tempReplacement + "", ",").trim());
}
return output;
}

调用:

String input = "\"aa\", \"a, aa\", [\"bb\", [\"1\", \"2\"]], [cc], [\"dd\", [\"5\"]]";
List<String> output = split(input, '#');

关于java - ["abc"、["123"、 "cba"的正则表达式]],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10896315/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com