1

我想拆分这个字符串

['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']

到java中的数组字符串数组?

我可以用正则表达式来做,还是应该写一个递归函数来处理这个目的?

4

4 回答 4

2

像下面这样的东西怎么样

String str= "['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']";

String[] arr = str.split("\\],\\[");
String[][] arrOfArr = new String[arr.length][];
for (int i = 0; i < arr.length; i++) {
    arrOfArr[i] = arr[i].replace("[", "").replace("]", "").split(",");
}
于 2012-09-29T11:05:16.617 回答
1

由于最近的崩溃清除了我所有的程序,我无法对此进行测试,但我相信您可以使用 JSON 解析器来解析字符串。在解析之前,您可能必须将其包装在[and]{and中。}

于 2012-09-29T12:01:35.787 回答
0
String yourString = "['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12.345','01-02-2012', 'Test 2']";
yourString = yourString.substring(1, yourString.lastIndexOf("]"));
String[] arr = yourString.split("\\],\\[");

String[][] arr1 = new String[arr.length][];
int i = 0;
String regex = "(?<=['],)";   // This regex will do what you want..
for(String a : arr) {
    arr1[i++] = a.split(regex);
}

for (String[] arrasd: arr1) {
    for (String s: arrasd) {
        System.out.println(s.replace(",", ""));
    }
}
于 2012-09-29T11:16:45.850 回答
0

您可以组合使用String.split和正则表达式后视

String str = "['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']";
String[] outerStrings = str.split("(?<=]),");
String[][] arrayOfArray = new String[outerStrings.length][];

for (int i=0; i < outerStrings.length; i++) {
   String noBrackets = outerStrings[i].substring(1, outerStrings[i].length() - 1);
   arrayOfArray[i] = noBrackets.split(",");
}
于 2012-09-29T11:27:34.443 回答