I want to split this string
['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']
To an array of array string in java?
Can I do it with regex or should I write a recursive function to handle this purpose?
I want to split this string
['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']
To an array of array string in java?
Can I do it with regex or should I write a recursive function to handle this purpose?
How about something like the following
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(",");
}
I'm not able to test this because of recent crash wiped out all my programs, but I believe you can use the JSON parsers to parse the string. You might have to wrap it in [ and ] or { and } before you parse.
See
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(",", ""));
}
}
You could use String.split and regex look-behind in combination:
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(",");
}