i've found an answer when searching in stackoverflow, it works perfectly but when i saw the comment the user doesn't explain how the code works, can someone explain to me how does this code means?....
String input =
" [John, 8, 9, 10, \r\n"
+ " , Peter, 10, 8, 7, \r\n"
+ " , Steve, 8, 9, 6, \r\n"
+ " ]";
String output = input.replaceAll("(?m)^[\\s,\\[]+|[\\s,\\]]+$|[\\s&&[^\r\n]]+", "");
System.out.println(output);
Answering your question from the comment:
"[\s&&[^\r\n]]+" this the part i dont understand at all, i don't get where the comma disappear while the is nocomma delimiter, and only bracket delimter" – Jansen Stanlie
I will ignore the string escapes and write it as true regex. In the java flavor of regex you can do class intersections and subtractions by using:
[..&&[..]] and [..&&[^..] respectively. Replace .. with your character class and inclusion/exclusion range.
For instance
[a-z&&[^aeiou]] would give you an english lowercase letter that is not a vowel.
So, in your example (removing java string escape) [\s&&[^\r\n]] - \s any whitespace character, except \r or \n. This was done to get rid of all spaces but keep the line breaks.
This could also be written as [\s&&[\V]] which would make it Unicode compliant. \V is any character that is NOT a vertical whitespace.
The question about where the comma is removed: The leading comma is removed here:
(?m)^[\\s,\\[]+|[\\s,\\]]+$|[\\s&&[^\r\n]]+
^
and trailing:
(?m)^[\\s,\\[]+|[\\s,\\]]+$|[\\s&&[^\r\n]]+
^