我想拆分这个字符串
['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']
到java中的数组字符串数组?
我可以用正则表达式来做,还是应该写一个递归函数来处理这个目的?
像下面这样的东西怎么样
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(",");
}
由于最近的崩溃清除了我所有的程序,我无法对此进行测试,但我相信您可以使用 JSON 解析器来解析字符串。在解析之前,您可能必须将其包装在[
and]
或{
and中。}
看
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(",", ""));
}
}
您可以组合使用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(",");
}